【发布时间】: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
【问题讨论】:
-
只是预感,下载的文件是不是和原来的一样大?
标签: asp.net content-type download mime-types