以zip格式下载文件 [英] download files in zip format

查看:97
本文介绍了以zip格式下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





这是我的代码,



Hi,

Here is my code,

string[] filePaths = Directory.GetFiles(@"D:\MP_Upload");
            string[] FName = new string[filePaths.Length];
            int j=0;
            foreach (string path in filePaths)
             {
                 FName[j] = Path.GetFileName(path);
                 j++;
            }
            using (ZipFile zip=new ZipFile())
            {
                for (int i = 0; i < FName.Length; i++)
                {
                    string filename = @"C:\Users\user\Downloads\" + FName[i];
                    string filePath = filePaths[i]; //Server.MapPath(@"D:\MP_Upload" + filename);
                  zip.AddFile(filePath, FName[i]);
                }
                Response.Clear();
                Response.BufferOutput = false;
                Response.AddHeader("Content-Disposition", "attachment; filename=chart4d(1).pdf");
                Response.ContentType = "application/zip";
                zip.Save(Response.OutputStream);
                Response.End();
            }





此代码的问题是只有一个文件作为普通文件下载。共有6个文件MP_upload文件夹。我希望所有这些文件都以zip格式下载。



the problem with this code is that only one file get download as normal file.There is total 6 files in MP_upload folder.I want all this files to downloaded in zip format.

推荐答案

使用此代码。



Use this code.

string SourceFolderPath = System.IO.Path.Combine(FolderPath, "Initial");

  Response.Clear();
  Response.ContentType = "application/zip";
  Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", nameFolder + ".zip"));

  bool recurseDirectories = true;
  using (ZipFile zip = new ZipFile())
  {
      zip.AddSelectedFiles("*", SourceFolderPath, string.Empty, recurseDirectories);
      zip.Save(Response.OutputStream);
  }
  Response.End();







解释如下链接



http://stackoverflow.com/questions/22564831/download-files-and-folders-as-a-zip-file-using-c-sharp-doesnt- work-in-mac [ ^ ]


这篇关于以zip格式下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆