I’m trying to download a ZIP file in ASP NET MVC. I have done in ASP NET Webforms, and it works correcly, but I do the same in MVC and I don’t get the same result, I tried the following:
public ActionResult Download()
{
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(Server.MapPath("~/Directories/hello"));
zip.Save(Server.MapPath("~/Directories/hello/sample.zip"));
return File(Server.MapPath("~/Directories/hello/sample.zip"),
"application/zip", "sample.zip");
}
}
But I get the binary data in screen, not the downloaded zip file why this is not working in MVC?
I have found that this does not work if I do it from a partial class, if I execute the download code from the Index and send the file if it works, why?
2
Answers
I use this to download files. In your view:
Where
GetMimeType
is a method that return known MIME types:With
_mappings
as:And the
TransmitStream
:Usually, if you want to download something i suggest you to use
ContentResult
,transforming the file you want to download into a Base64 String and transforming it on the frontend using javascript with a Blob
Action
front end