I have implemented a function in an .NET application to download a large number of files using a stream with an undefined initial length. I need to capture, on the browser, when the stream ends to show an alert to the user but I have difficulty understanding how to solve or which workaround to use.
This is my function:
private void OutputStreamZipped(Page page)
{
page.Response.ContentType = "application/zip";
page.Response.AddHeader("content-disposition", "attachment" + ";filename=" + "myFileName.zip");
page.Response.AddHeader("Accept-Ranges", "bytes");
page.Response.AddHeader("Expires", "0");
page.Response.AddHeader("Pragma", "cache");
page.Response.AddHeader("Cache-Control", "private");
page.Response.Buffer = false;
page.Response.BufferOutput = false;
byte[] buffer = new byte[2 * 1024 * 1024];
try
{
using (ZipOutputStream zipOutputStream = new ZipOutputStream(Response.OutputStream))
{
zipOutputStream.SetLevel(3);
DirectoryInfo DI = new DirectoryInfo(@"C:\myFolder");
foreach (var i in DI.GetFiles())
{
Stream fs = File.OpenRead(i.FullName);
ZipEntry zipEntry = new ZipEntry(ZipEntry.CleanName(i.Name));
zipEntry.Size = fs.Length;
zipOutputStream.PutNextEntry(zipEntry);
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;
}
Response.Flush();
}
fs.Close();
}
zipOutputStream.Close();
}
Response.Flush();
page.Response.SuppressContent = true;
System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception)
{
throw;
}
}
Thanks to anyone who can give me a tip