Skip to content

Instantly share code, notes, and snippets.

@fex80
Last active September 30, 2021 13:57
Show Gist options
  • Select an option

  • Save fex80/10478783 to your computer and use it in GitHub Desktop.

Select an option

Save fex80/10478783 to your computer and use it in GitHub Desktop.
Return a zip file on the fly from a ASP.NET WebApi ApiController
using Ionic.Zip; // from NUGET-Package "DotNetZip"
public HttpResponseMessage AllZipped()
{
using (var zipFile = new ZipFile())
{
// zipFile.AddEntry(...);
// zipFile.AddEntry(...);
return ZipContentResult(zipFile);
}
}
protected HttpResponseMessage ZipContentResult(ZipFile zipFile)
{
// inspired from http://stackoverflow.com/a/16171977/92756
var pushStreamContent = new PushStreamContent((stream, content, context) =>
{
zipFile.Save(stream);
stream.Close(); // After save we close the stream to signal that we are done writing.
}, "application/zip");
return new HttpResponseMessage(HttpStatusCode.OK) {Content = pushStreamContent};
}
@ranjith88

Copy link
Copy Markdown

How can we control the zip file name here

@rabindra0388

Copy link
Copy Markdown

How can we rename file name lets say testZip.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment