2015-04-17 95 views
0

我试图搜索并下载上传到数据库的文件,我可以从数据库中检索文件。 下载单个文件的作品,但无法一次下载多个文件,我已阅读关于下载为zip文件,任何人都可以帮助??????Asp.net多个文件下载

 using (sampledbase dbcontext = new sampledbase()) 
     { 

      ZipFile zip = new ZipFile(); 
      long id = Convert.ToInt64(Request.QueryString["id"]); 
      System.Nullable<long> fileid = id; 
      var query = dbcontext.searchfile(ref fileid); 

      foreach (var i in query) 
      { 
       byte[] binarydata = i.Data.ToArray(); 
       Response.AddHeader("content-disposition", string.Format("attachment; filename =\"{0}\"", i.Name)); 
       Response.BinaryWrite(binarydata); 
       Response.ContentType = i.ContentType; 
       Response.End(); 
      } 
     } 
+0

为什么你不能将文件添加到Zip文件并下载Zip文件而不是多个文件? –

+0

如何从数据库中压缩所有文件? – user1747254

+0

我的方法是创建隐藏链接或表单标签,并使用它们将请求发送到所需的文件并一个接一个地下载它们 – Kira

回答

0

我看到您正在使用ZipFile,因此您处于正确的方向。为了压缩文件,您可以保存文件的目录,并使用此代码压缩的目录:

string startPath = @"c:\example\start"; 
string zipPath = @"c:\example\result.zip"; 
ZipFile.CreateFromDirectory(startPath, zipPath); 

在你的情况,看看通过dbcontext.searchfile(返回的数据类型),并保存内容到使用StreamWriter之类的目录。看你的代码,类型似乎是一个byte [],你可以看到这个链接,了解如何将字节保存在一个文件中:Write bytes to file

另一种解决方案是直接压缩字节[]:Create zip file from byte[]