0

I'm trying to return a zipped file

public FileResult Download()
        {
                MemoryStream outputStream = new MemoryStream();

                using (ZipFile zip = new ZipFile())
                {
                    zip.AddEntry("asdasd.html", "<html>fgdfg</html>");
                    zip.AddEntry("asdassssd.html", "<html>asddsaf</html>");
                    zip.Save(outputStream);
                }

               return File(outputStream, "application/zip", "file.zip");
}

but in response it returns an error (in XML):

XML Parsing Error: no element found Location: moz-nullprincipal:{122aa411-1418-43f5-b950-4347af7c7217} Line Number 1, Column 1:

What is wrong with my response (to zip files i use DotNetZip)?

1gn1ter
  • 1,414
  • 2
  • 22
  • 47

1 Answers1

3

You probably need to reset the MemoryStream to the beginning of its buffer before you return it to the client as a File.

outputStream.Seek(0, SeekOrigin.Begin);

I also suggest you use a proxy like Fiddler to inspect the Http response to get a better handle on what exactly your request is sending.

Mike Haboustak
  • 2,266
  • 1
  • 18
  • 18
  • Still no file.. In Fiddler in RawView: HTTP/1.1 200 OK Server: ASP.NET Development Server/10.0.0.0 Date: Wed, 24 Aug 2011 11:25:56 GMT X-AspNet-Version: 4.0.30319 X-AspNetMvc-Version: 3.0 Content-Disposition: attachment; filename=hey.zip Cache-Control: private, s-maxage=0 Content-Type: application/zip Content-Length: 422 Connection: Close PK���� – 1gn1ter Aug 24 '11 at 11:24
  • I have found the problem: I should call window.location.href = '@Url.Action("Download", "File")?params=' + allVals; rather then $.post (....) And of cause to add outputStream.Seek(0, SeekOrigin.Begin); Thank you! – 1gn1ter Aug 24 '11 at 14:08