2010-03-16 164 views
4
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(); 

实际的压缩文件c:\ temp \ test.zip是好的,有效的,无论您想调用它。当我导航到目录c:\ temp \并双击test.zip文件时;它正确地打开。ASP.NET创建zip文件进行下载:压缩的压缩文件夹无效或损坏

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

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

对于Response.ContentType我试着:

应用程序/ x压缩 应用程序/ x-ZIP压缩 应用程序/ x-用gzip compresse 应用/八位字节流 应用程序/压缩

Ionic.zip

http://www.codeplex.com/DotNetZip

:使用

的zip文件正在被一些现有的代码创建了(我敢肯定,工作正常,由于我直接打开所创建的文件的能力)

+0

只是一个预感,下载的文件是否与原始大小相同? – CResults 2010-03-16 18:22:30

回答

21

这工作。我不知道为什么,但它确实如此。

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(); 
+0

也在这里工作,谢谢。这个问题似乎是两个:Response.AppendHeader(“Content-Length”,file.Length.ToString())和 Response.Flush()它没有与一个或另一个工作,但没有这两个工作。有人可以解释这一点?无论如何,谢谢杰森:) – Nei 2011-10-26 12:16:50