I'm trying to steam file in my web application from another domain and I'll zip this files for a download. For this I use Ionic Zip. But I've got this error on the marked line in my code:
System.ObjectDisposedException
: No access to a closed Stream.
Here is my code. I'm using C# in an ASP.NET MVC application.
using Ionic.Zip;
using System.IO;
using System.Net;
[HttpPost]
public async Task<ActionResult> Downloads(string lang, string product, IEnumerable<string> file, string action)
{
string zipname = "manuals.zip";
using (ZipFile zip = new ZipFile())
{
foreach (string f in file.Distinct())
{
using (WebClient client = new WebClient())
{
using (MemoryStream output = new MemoryStream())
{
byte[] b = client.DownloadData(f);
await output.WriteAsync(b, 0, b.Length);
await output.FlushAsync();
output.Position = 0;
zip.AddEntry(f.Split('/').Last(), output);
output.Close();
}
}
}
Response.Clear();
Response.ContentType = "application/zip, application/octet-stream";
Response.AddHeader("content-disposition", $"attachment; filename={product.Replace('/', '-')}-{zipname}");
zip.Save(Response.OutputStream); // ← error on this line
Response.End();
}
}
What's wrong with this code?