2012-03-13 66 views
0

當我有這個小碼:空的檔案壓縮與ZipForge多個文件和C#

HttpContext.Current.Response.Clear(); 

if (files.Contains(',')) { 
    HttpContext.Current.Response.ContentType = "application/zip"; 
    HttpContext.Current.Response.AddHeader("content-disposition", "filename=arch-01.zip"); 

    string[] split = files.Split(','); 
    ZipForge zip = new ZipForge(); 
    try { 
     zip.FileName = Path.Combine(@"C:\Item", "arch-01.zip"); 
     zip.OpenArchive(System.IO.FileMode.Create); 
     zip.BaseDir = @"C:\"; 

     foreach (string file in split) { 
      zip.AddFiles(file); 
     } 

     zip.CloseArchive(); 

    } catch (Exception e) { 
     Console.Write(e.Message); 
    } 
} 

壓縮,顯示在瀏覽器Open - Save As對話框後,我想。該文件存在,我們假設files包含

C:\Item\a1.xls,C:\Item\a2.xls. 

但歸檔是空的,永遠。我的錯誤在哪裏?我有C:的許可。 我在響應頭錯了嗎?

HttpContext.Current.Response.WriteFile(zip.FileName); 

和現在的作品:)

回答

0

我會嘗試拆分出你的問題出找出問題的所在:

問題下面的代碼解決

我已經加入。製作一個控制檯應用程序,用於壓縮您想要壓縮的文件並查看是否有效。之後,看看你是否可以下載你創建的壓縮文件,並且正確地從你的asp.net網站上下載壓縮文件。代碼中似乎有兩件事情沒有問題,但我們需要確定實際問題的位置。

0

我猜你缺少你的響應頭附加。 它應該是這樣的

HttpContext.Current.Response.AddHeader(「Content-Disposition」,「attachment; filename = arch-01.zip」);

這應該可以解決我想這個問題。

+0

我知道'attachment',但它不工作無論如何 – 2012-03-14 07:11:18