温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在asp.net中实现同时下载多个文件

发布时间:2021-02-08 17:23:48 来源:亿速云 阅读:181 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关如何在asp.net中实现同时下载多个文件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

1、首先读取文件夹下的文件,可能同时存在多个文件

2、选中文件,然后点击下载,同时可以选择多个文件。

思路:通过生产压缩包的形式进行下载,然后再清楚压缩包,这样用户可以一次性全部下载下来。

一、获取目录下的所有文件,然后绑定到checkboxlist中 ,代码如下:

 ckl_ck.Items.Clear();
 DirectoryInfo TheFolder = new DirectoryInfo(Server.MapPath("Resource/Help"));
 //遍历文件夹下的文件
 foreach (FileInfo NextFile in TheFolder.GetFiles())
  this.ckl_ck.Items.Add(NextFile.Name);

二、选中文件后,点击下载按钮。代码:

 protected void Btn_down_Click(object sender, EventArgs e)
 {
 if (ckl_ck.Items.Count > 0)
 {
  List<string> listFJ = new List<string>();//保存附件路径
  List<string> listFJName = new List<string>();//保存附件名字
  for (int i = 0; i < ckl_ck.Items.Count; i++)
  {
  if (ckl_ck.Items[i].Selected)
  {
   listFJ.Add(Server.MapPath("Resource/Help/") + ckl_ck.Items[i].Text);
   listFJName.Add(ckl_ck.Items[i].Text);
  }

  }
  string time = DateTime.Now.Ticks.ToString();
  ZipFileMain(listFJ.ToArray(), listFJName.ToArray(), Server.MapPath("Resource/Help/" + time + ".zip"), 9);//压缩文件
  DownloadFile(Server.UrlEncode("附件.zip"), Server.MapPath("Resource/Help/" + time + ".zip"));//下载文件
 }
 }
 private void DownloadFile(string fileName, string filePath)
 {
 FileInfo fileInfo = new FileInfo(filePath);
 Response.Clear();
 Response.ClearContent();
 Response.ClearHeaders();
 Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
 Response.AddHeader("Content-Length", fileInfo.Length.ToString());
 Response.AddHeader("Content-Transfer-Encoding", "binary");
 Response.ContentType = "application/octet-stream";
 Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
 Response.WriteFile(fileInfo.FullName);
 Response.Flush();
 File.Delete(filePath);//删除已下载文件
 Response.End();
 }

 /// <summary>
 /// 压缩文件
 /// </summary>
 /// <param name="fileName">要压缩的所有文件(完全路径)</param>
 /// <param name="fileName">文件名称</param>
 /// <param name="name">压缩后文件路径</param>
 /// <param name="Level">压缩级别</param>
 public void ZipFileMain(string[] filenames, string[] fileName, string name, int Level)
 {
 ZipOutputStream s = new ZipOutputStream(File.Create(name));
 Crc32 crc = new Crc32();
 //压缩级别
 s.SetLevel(Level); // 0 - store only to 9 - means best compression
 try
 {
  int m = 0;
  foreach (string file in filenames)
  {
  //打开压缩文件
  FileStream fs = File.OpenRead(file);//文件地址
  byte[] buffer = new byte[fs.Length];
  fs.Read(buffer, 0, buffer.Length);
  //建立压缩实体
  ZipEntry entry = new ZipEntry(fileName[m].ToString());//原文件名
  //时间
  entry.DateTime = DateTime.Now;
  //空间大小
  entry.Size = fs.Length;
  fs.Close();
  crc.Reset();
  crc.Update(buffer);
  entry.Crc = crc.Value;
  s.PutNextEntry(entry);
  s.Write(buffer, 0, buffer.Length);
  m++;
  }
 }
 catch
 {
  throw;
 }
 finally
 {
  s.Finish();
  s.Close();
 }
 }

三、系统中需要引用的dll 需要下载。

四、运行效果如图:

如何在asp.net中实现同时下载多个文件

看完上述内容,你们对如何在asp.net中实现同时下载多个文件有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI