我正在尝试从另一个域在我的 Web 应用程序中传输文件,我将压缩这些文件以供下载。为此,我使用 Ionic Zip。但是我的代码中的标记行出现了这个错误:
System.ObjectDisposedException
:无法访问已关闭的 Stream。
这是我的代码。我在 ASP.NET MVC 应用程序中使用 C#。
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();
}
}
这段代码有什么问题?