2014-10-01 60 views
-1

Iam在提名门户上工作。用户提交他们的提名和一些附件。每个提名的附件数量可能在3到10之间。因此,对于每一次提名,评估人员必须每次下载10个文件,这非常紧张。所以我想提供一个选项,一次下载与提名有关的所有附件作为Zip文件。我能够一次下载单个文件。请为我提供一个解决方案,一次下载多个文件以及一些示例代码片段。以下是我用于下载文件下载的单个文件 的代码。我将文件内容存储为数据库中的二进制文件使用C从数据库下载多个文件

protected void DownloadFile(object sender, EventArgs e) 
{ 
    int id = int.Parse((sender as LinkButton).CommandArgument); 
    byte[] bytes; 
    string fileName, contentType; 
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; 
    using (SqlConnection con = new SqlConnection(constr)) 
    { 
     using (SqlCommand cmd = new SqlCommand()) 
     { 
      cmd.CommandText = "select Name, Data, ContentType from tblFiles where [email protected]"; 
      cmd.Parameters.AddWithValue("@Id", id); 
      cmd.Connection = con; 
      con.Open(); 
      using (SqlDataReader sdr = cmd.ExecuteReader()) 
      { 
       sdr.Read(); 
       bytes = (byte[])sdr["Data"]; 
       contentType = sdr["ContentType"].ToString(); 
       fileName = sdr["Name"].ToString(); 
      } 
      con.Close(); 
     } 
    } 
    Response.Clear(); 
    Response.Buffer = true; 
    Response.Charset = ""; 
    Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    Response.ContentType = contentType; 
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName); 
    Response.BinaryWrite(bytes); 
    Response.Flush(); 
    Response.End(); 
} 
+0

您需要格式化您的代码。 – artm 2014-10-01 04:34:01

+0

实际问题是什么?如何创建一个zip文件?你有没有尝试过一些东西?你尝试谷歌,并没有找到解决方案? – 2014-10-01 07:08:12

+0

您好Kanavos我的实际问题是如何从数据库一次下载多个文件。压缩是第二优先。即使我能够将所有文件存储在文件夹中,那也很好。我使用了Google,但我没有找到合适的解决方案。 Google中的每个解决方案都基于获取托管在服务器中但不在数据库中的一组文件,这与我的要求完全不同 – shabareesh 2014-10-01 07:17:26

回答

1

您没有提及您使用的是什么版本的.NET。

在.NET 4.5,你可以使用ZipFile类:ZipFile Class

写入到一个zip压缩文件的例子如下所示:ZipArchive.CreateEntry

更新:

替代了早期版本的.NET是:SharpZipLib

+0

[ZipArchiveEntry.Open](http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchiveentry.open(v = vs.110).aspx)的文档甚至包含一个示例如何创建一个条目并在不首先保存到磁盘的情况下写入它 – 2014-10-01 07:09:04

+0

嗨Green,我的.net版本是3.5 – shabareesh 2014-10-01 07:16:25