5

我尝试下载由Ionic.Zip.dllasp.net c# web 表单应用程序制作的 zip 文件,如下所示:

zip.AddEntry("filename", arraybyte);
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=SuppliersDocuments.zip");
zip.Save(Response.OutputStream);
Response.Close();

但我得到Failed - network error这样的:

在此处输入图像描述

错误只发生在chrome中,它在其他浏览器中正常工作。我的本地主机上没有发生错误,它只发生在主服务器上。

如果有人可以解释这个问题的解决方案,那将非常有帮助。

4

1 回答 1

1

我有同样的问题,这发生在 Chrome 中。此问题发生在 Chrome v53 及更高版本上,此处对此进行了说明。

为了克服这个问题,我建议您获取文件的长度并在标题中使用 Content-Length 并传递文件的长度。

string fileName = string.Format("Download.zip");
Response.BufferOutput = false;  // for large files  
using (ZipFile zip = new ZipFile())
{
      zip.AddFile(path, "Download");
      Response.Clear();
      Response.Buffer = true;
      Response.ContentType = "application/zip";
      Response.AddHeader(name: "Content-Disposition", value: "attachment;filename=" + fileName);
      Int64 fileSizeInBytes = new FileInfo(path).Length;
      Response.AddHeader("Content-Length", fileSizeInBytes.ToString());                                    
      zip.Save(Response.OutputStream);
      Response.Flush();
}
Response.Close();
于 2017-08-16T14:44:28.440 回答