1

I'm using the DotNetZip MVC Extension Method example for adding multiple files (I am getting mine from a repository) but I can't seem to figure out how to pass my own fileName into the extension method and get a result other than "file.zip", which is their examples hardcoded default value. Below is my CSHTML code, my Action and my Extension Method. You will see in my Action that I have a filename I want to use.

I'm embarrassed to show my attempts, but you can see what I'd like to use for my filename. Any suggestions?

CSHTML (Razor)

<a href="/Renders/Download/@renders.RenderId">Download</a>

Controller Action:

    public ActionResult Download(int id)
    {
        var allImages = _repo.GetImagesByRender(id);
        var list = new List<String>();

        var render = _repo.GetRenderById(id);
        var fileName = render.Select(r => r.Title);

        foreach (var img in allImages)
        {
            list.Add(Server.MapPath("~/ImageStore/" + img.Path));
        }

        return new ZipResult(list);
    }

The Extension Method

public class ZipResult : ActionResult
{
    private IEnumerable<string> _files;
    private string _fileName;

    public string FileName
    {
        get
        {
            return _fileName ?? "file.zip";
        }
        set { _fileName = value; }
    }

    public ZipResult(params string[] files)
    {
        this._files = files;
    }

    public ZipResult(IEnumerable<string> files)
    {
        this._files = files;
    }

    public override void ExecuteResult(ControllerContext context)
    {   // using clause guarantees that the Dispose() method is called implicitly!
        using (ZipFile zf = new ZipFile())
        {
            zf.AddFiles(_files, false, "");
            context.HttpContext.Response
                   .ContentType = "application/zip";
            context.HttpContext.Response
                   .AppendHeader("content-disposition", "attachment; filename=" + FileName);

            zf.Save(context.HttpContext.Response.OutputStream);
        }
    }

} 

As for the Repo, it returns the proper Images collection associated by RenderId and also the propper Render so that I can use the Render Title as the fileName, but how would I modify the ACtion and the Extended Action Method in order to make my zipFile have the proper name?

1 Answer 1

2

You can add another constructor to your ZipResult class:

...
public ZipResult(IEnumerable<string> files, string fileName)
{
    this._files = files;
    this._fileName = fileName;
}
...

Then in controller you cne use it:

...
return new ZipResult(list, "test.zip");
3
  • I tried something similar to this. Let me look at my code again. I think I see what I did wrong....I think I was forgetting to cast my fileName assignment value ToString().... Dec 19, 2014 at 9:45
  • Sometimes you need someone to point out the obvious. The reason I was getting an error was because I was trying to pass something that wasn't a string into the method. I really appreciate your help! I realized once I saw your "test.zip" that it needed to be a string. Dec 19, 2014 at 9:57
  • I tried it on new project and it works perfect. I just ziped static file form Content directory.
    – py3r3str
    Dec 19, 2014 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.