登录  /  注册

有关asp.net如何实现多个文件同时下载问题相关解答

巴扎黑
发布: 2018-05-18 16:45:33
原创
1404人浏览过

这篇文章主要为大家详细介绍了asp.net实现多个文件同时下载功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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如何实现多个文件同时下载问题相关解答的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号