c# - C# Response.OutputStream 损坏下载的文件
问题描述
我正在尝试创建动态创建的可下载 ZIP 文件。这些文件是在服务器上正确创建的,但是当它们使用“Response.OutputStream”发送到客户端时,文件会损坏。这似乎仅在 zip 文件超过 4GB 时发生。有谁知道这是为什么?
我使用的确切代码是:
string path = @"C:\temp\vid";
Response.BufferOutput = false; // Disable Buffer Output to start the download immediately
// Set custom headers to force browser to download the file instad of trying to open it
Response.ContentType = "application/x-zip-compressed";
Response.AppendHeader("content-disposition", "attachment; filename=Archive.zip");
ZipOutputStream zipOutputStream = new ZipOutputStream(Response.OutputStream, 20000);
zipOutputStream.SetLevel(0); // No compression
zipOutputStream.UseZip64 = UseZip64.On;//Forces Zip64 to be used
zipOutputStream.IsStreamOwner = true;
try
{
foreach (string file in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories))
{
using (var fs = System.IO.File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
ZipEntry entry = new ZipEntry(ZipEntry.CleanName(Path.GetFileName(file))); //Create zip compatible name
zipOutputStream.PutNextEntry(entry); //Adds entry to list
fs.CopyTo(zipOutputStream);
zipOutputStream.CloseEntry();
zipOutputStream.Flush();
Response.Flush();
Response.Clear();
}
}
zipOutputStream.Finish();
zipOutputStream.Close();
Response.End();
Response.Flush();
}
catch
{
Debug.WriteLine("Connection Closed or error");
}
return new HttpStatusCodeResult(HttpStatusCode.OK);
解决方案
推荐阅读
- c# - 带有左外连接的 LINQ 查询
- oracle - 将 Oracle 部署为 WSO2 APIM 的数据库时出现 ORA-12705 NLS_LANG 错误
- networking - Centos 8 e podman 网络
- kubernetes - “使密钥保密”失败并出现错误“make:***没有规则来制作目标‘密钥’。停止。”
- azure-active-directory - 如何在 OpenIdConnectConfigurationRetriever.GetAsync 调用中使用公司代理?
- javascript - 我得到“无法设置属性‘innerHTML’为空
- javascript - Bootstrap 4导航下拉悬停在桌面/点击移动
- asp.net-mvc - aspnet mvc 页面在未使用几分钟后崩溃
- javascript - $('#iframe').load() 在生产中响应很晚,尽管它在开发(本地)环境中运行良好
- php - 仅在 is_archive woocommerce 时显示价格