SharpZipLib
SharpZipLib copied to clipboard
Zip-as-a-browser-download-attachment doesnt work in .Net Core 2.0
The sample for creating a zip fie as a browser download attachment as mentioned at doesnt seem to work in .Net Core 2.0:
https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples#create-a-zip-as-a-browser-download-attachment-in-iis
In the sample code, I think Response.Flush();
need to be updated to Response.Body.Flush();
whereas Response.End();
, need to be removed.
But I am not sure what Response.OutputStream
should be updated with? It seems that in .net core 2.0 Response.OutputStream
is not available.
If I right understand the body stream ist the output strem of the response. But I'm not shure and have the same problem that the samples not work for me :(
I adapted the code to ASP.NET Core 2.1 for a repro:
[Route("api/[controller]")]
[ApiController]
public class Repro194Controller : ControllerBase
{
[Route("")]
public IActionResult DownloadZipToBrowser()
{
byte[] buffer = new byte[4096];
var ms = new MemoryStream();
using (var zipOutputStream = new ZipOutputStream(ms))
{
zipOutputStream.IsStreamOwner = false;
zipOutputStream.SetLevel(3); //0-9, 9 being the highest level of compression
foreach (string fileName in Repro194.ZipFileList)
{
using (var fs = System.IO.File.OpenRead(fileName)) // or any suitable inputstream
{
var entry = new ZipEntry(ZipEntry.CleanName(fileName));
entry.Size = fs.Length;
// Setting the Size provides WinXP built-in extractor compatibility,
// but if not available, you can set zipOutputStream.UseZip64 = UseZip64.Off instead.
zipOutputStream.PutNextEntry(entry);
int count = fs.Read(buffer, 0, buffer.Length);
while (count > 0)
{
zipOutputStream.Write(buffer, 0, count);
count = fs.Read(buffer, 0, buffer.Length);
//if (!Response.IsClientConnected) break;
ms.Flush();
}
}
}
zipOutputStream.Flush();
}
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
return File(ms, "application/zip", "Download.zip");
}
}
It seems to work as intended, although an update to the documentation is long over due.
Thanks for helping to maintain and develop this library. Really want to stress the importance of getting this updated in the docs, ASP .NET Core 2 is only going to be used by more developers and it took a lot of searching to find this.
I am not really sure how this is supposed to work, my code was just an attempt at reproducing the issue. If you have any good ideas about how to improve the documentation you can edit it directly on github.
I agree that it's important, we just need to find someone to do it :)