【问题标题】:Asp.net Multiple files downloadAsp.net 多文件下载
【发布时间】:2015-06-24 03:34:09
【问题描述】:

我正在尝试搜索和下载上传到数据库的文件,我可以从数据库中检索文件。 下载单个文件有效,但不能一次下载多个文件,我已经阅读了有关下载为 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();
           }
       }

【问题讨论】:

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

标签: asp.net


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 2010-12-29
    相关资源
    最近更新 更多