Asp.net多文件下载

Asp.net多文件下载,asp.net,Asp.net,我试图搜索和下载上传到数据库的文件,我可以从数据库检索文件。 下载单个文件很有效,但不能一次下载多个文件,我读过关于作为zip文件下载的文章,有人能帮忙吗 using (sampledbase dbcontext = new sampledbase ()) { ZipFile zip = new ZipFile(); long id = Convert.ToInt64(Request.QueryString["id"]);

我试图搜索和下载上传到数据库的文件,我可以从数据库检索文件。 下载单个文件很有效,但不能一次下载多个文件,我读过关于作为zip文件下载的文章,有人能帮忙吗

     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();
           }
       }
使用(sampledbase dbcontext=newsampledbase())
{
ZipFile zip=新ZipFile();
long id=Convert.ToInt64(Request.QueryString[“id”]);
System.Nullable fileid=id;
var query=dbcontext.searchfile(ref fileid);
foreach(查询中的变量i)
{
字节[]二进制数据=i.Data.ToArray();
AddHeader(“内容处置”,string.Format(“附件;文件名=\”{0}\”,i.Name));
BinaryWrite(binarydata);
Response.ContentType=i.ContentType;
Response.End();
}
}

我看到您正在使用ZipFile,所以您的方向是正确的。为了压缩文件,您可以将文件保存在目录中,并使用以下代码压缩目录:

string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";
ZipFile.CreateFromDirectory(startPath, zipPath);
在您的例子中,查看dbcontext.searchfile()返回的数据类型,并使用类似StreamWriter的方法将内容保存到目录中。查看您的代码,类型似乎是byte[],您可以查看此链接以了解如何将字节保存在文件中:


另一个解决方案是直接压缩字节[]:

为什么不能将文件添加到zip文件中并下载zip文件而不是多个文件?如何从数据库压缩所有文件???我的方法是创建隐藏链接或表单标签,并使用它们将请求发送到所需文件,然后逐个下载它们