【问题标题】:ASP.NET Create zip file for download: the compressed zipped folder is invalid or corruptedASP.NET 创建 zip 文件以供下载:压缩的压缩文件夹无效或损坏
【发布时间】:2011-01-28 05:58:22
【问题描述】:
string fileName = "test.zip";
string path = "c:\\temp\\";
string fullPath = path + fileName;
FileInfo file = new FileInfo(fullPath);

Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
Response.AppendHeader("content-length", file.Length.ToString());
Response.ContentType = "application/x-compressed";
Response.TransmitFile(fullPath);
Response.Flush();
Response.End();

实际的 zip 文件 c:\temp\test.zip 是好的、有效的,不管你想怎么称呼它。当我导航到目录 c:\temp\ 并双击 test.zip 文件时;它会立即打开。

我的问题似乎只与下载有关。上面的代码执行没有任何问题。出现文件下载对话框。我可以选择保存或打开。如果我尝试从对话框中打开文件,或者保存它然后打开它。我收到以下对话框消息:

压缩(压缩)文件夹无效或损坏。

对于 Response.ContentType 我试过了:

应用程序/x-压缩 应用程序/x-zip-压缩 应用程序/x-gzip-压缩 应用程序/八位字节流 应用程序/压缩包

使用一些先前的代码创建 zip 文件(由于我能够直接打开创建的文件,我确信它工作正常)使用:Ionic.zip

http://www.codeplex.com/DotNetZip

【问题讨论】:

  • 只是预感,下载的文件是不是和原来的一样大?

标签: asp.net content-type download mime-types


【解决方案1】:

这行得通。我不知道为什么,但确实如此。

string fileName = "test.zip";
string path = "c:\\temp\\";
string fullPath = path + fileName;
FileInfo file = new FileInfo(fullPath);

Response.Clear();
//Response.ClearContent();
//Response.ClearHeaders();
//Response.Buffer = true;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
//Response.AppendHeader("Content-Cength", file.Length.ToString());
Response.ContentType = "application/x-zip-compressed";
Response.WriteFile(fullPath);
//Response.Flush();
Response.End();

【讨论】:

  • 也在这里工作,谢谢。问题似乎是两个: Response.AppendHeader("Content-Length", file.Length.ToString()) 和 Response.Flush() 它不能与一个或另一个一起工作,但没有两者都可以工作。有人可以解释一下吗?无论如何谢谢杰森:)
猜你喜欢
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
  • 2011-02-21
  • 1970-01-01
  • 2015-01-19
  • 1970-01-01
  • 2020-01-09
相关资源
最近更新 更多