2015-06-08 23 views
-4

在這裏我有幾個圖像,我需要壓縮這些圖像,並需要下載它..在這裏我使用Ionzip。問題是zip不工作,它不顯示我有任何錯誤。zip does'n讓我看到任何問題

mycode的

public bool DownloadImgs() 
     { 


     string Path = HttpContext.Current.Server.MapPath("../Content/images/QImages"); 
     string zippath = HttpContext.Current.Server.MapPath("../Content/images/QImages/zipped/"); 
     string[] filenames = System.IO.Directory.GetFiles(Path, "*.jpg", SearchOption.AllDirectories);//It returns all the paths of the images. 

     using (ZipFile zip = new ZipFile()) 
     { 
      foreach (String filename in filenames) 
      { 
       ZipEntry e = zip.AddFile(filename, ""); 
      } 
      zip.Save(zippath);//In here i need to download the zipped file.not to save 
     } 
} 

PS:此應用程序使用MVC框架

+1

所以zip不會生成,但是你要下載它呢? –

+0

@StevenLemmens No.zip文件沒有創建..但是我需要下載它 – UDP

回答

2

你應該寫所產生的壓縮到Response流建。

從MVC控制器:

return this.File(zippath, "application/zip"); 

從ASP.NET處理程序或頁面:

Response.TransmitFile(zippath); 

另一種方法是直接將ZIP文件保存到響應流,這將優化磁盤使用情況。

+0

沒有響應關鍵字 – UDP

+0

您是否使用MVC的第一行? –

+0

這是mvc app。在我的控制器中調用這個函數 – UDP

0

您可以將ZIP文件保存直奔響應的OutputStream:

zipFile.Save(Response.OutputStream); 
+0

沒有任何Response關鍵字..是HttpResponse嗎? – UDP

+0

@UDP:位於ASP.NET Web窗體或HTTP處理程序中。對於MVC,你需要別的東西。 –

+0

是的。對不起,我沒有注意到他正在使用MVC –