Last active
September 30, 2021 13:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can we rename file name lets say testZip.zip