2010-03-16 132 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