当前位置: 首页 > news >正文

ASP.NET批量下载文件

一、实现步骤


  在用户操作界面,由用户选择需要下载的文件,系统根据所选文件,在服务器上创建用于存储所选文件的临时文件夹,将所选文件拷贝至临时文件夹。然后调用 RAR程序,对临时文件夹进行压缩,然后输出到客户端。最后删除临时文件夹。

 

二、代码实现

 
 
1、ASP.NET批量下载 核心代码
 
 
C# 代码    复制
//遍历服务器指定文件夹下的所有文件
 string path = "uploads/Image/"; string serverPath = Server.MapPath(path); //创建临时文件夹  string tempName = DateTime.Now.ToString("yyyyMMddHHMMss"); string tempFolder = Path.Combine(serverPath, tempName); Directory.CreateDirectory(tempFolder); DirectoryInfo folder = new DirectoryInfo(serverPath); foreach (FileInfo file in folder.GetFiles()) { string filename = file.Name; File.Copy(serverPath + "/" + filename, tempFolder + "/" + filename); } //ZKHelper.JSHelper.Alert("图片拷贝成功!"); //产生RAR文件,及文件输出  RARSave(tempFolder, tempName); DownloadRAR(tempFolder + "\\\\" + tempName + ".rar"); 

 

2、RARSave(string tempFolder, string tempName) 方法

 

 
C# 代码    复制
/// <summary> /// 生成RAR文件 /// </summary> /// <param name="path">存放复制文件的目录</param> /// <param name="rarPatch">RAR文件存放目录</param> /// <param name="rarName">RAR文件名</param>  private void RARSave(string rarPatch, string rarName) { string the_rar; RegistryKey the_Reg; Object the_Obj; string the_Info; ProcessStartInfo the_StartInfo; Process the_Process; try { the_Reg = Registry.ClassesRoot.OpenSubKey(@"WinRAR"); the_Obj = the_Reg.GetValue(""); the_rar = the_Obj.ToString(); the_Reg.Close(); the_rar = the_rar.Substring(1, the_rar.Length - 7); the_Info = " a " + rarName + " -r"; the_StartInfo = new ProcessStartInfo(); the_StartInfo.FileName = "WinRar";//the_rar;  the_StartInfo.Arguments = the_Info; the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //打包文件存放目录  the_StartInfo.WorkingDirectory = rarPatch; the_Process = new Process(); the_Process.StartInfo = the_StartInfo; the_Process.Start(); the_Process.WaitForExit(); the_Process.Close(); } catch (Exception) { throw; } } 

 

3、DownloadRAR(string file)方法

 

 
C# 代码    复制
/// <summary> /// 下载生成的RAR文件 /// </summary>  private void DownloadRAR(string file) { FileInfo fileInfo = new FileInfo(file); Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name); 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(); string tempPath = file.Substring(0, file.LastIndexOf("\\\\")); //删除临时目录下的所有文件  DeleteFiles(tempPath); //删除空目录  Directory.Delete(tempPath); Response.End(); } 


4、DeleteFiles(string tempPath) 方法

 

 
C# 代码    复制
/// <summary> /// 删除临时目录下的所有文件 /// </summary> /// <param name="tempPath">临时目录路径</param>  private void DeleteFiles(string tempPath) { DirectoryInfo directory = new DirectoryInfo(tempPath); try { foreach (FileInfo file in directory.GetFiles()) { if (file.Attributes.ToString().IndexOf("ReadOnly") != -1) { file.Attributes = FileAttributes.Normal; } File.Delete(file.FullName); } } catch (Exception) { throw; } } 

相关文章:

  • VirtualBox设置CentOS虚拟机双网卡
  • 宝宝头三年至关重要,不看悔掉肠子
  • apache内存优化
  • 此类目的是防治序列化Json字符串时的循环引用问题-------最好解决方案
  • Spring AOP 实现业务日志记录 (注解)
  • 必须知道的SQL编写技巧,多条件查询不拼字符串的写法
  • Mysql高级之权限检查原理
  • 我见过的 Objective-C, 讲的最通俗易懂的入门教程....
  • 数据结构概述
  • 关于与产品相关的品牌、国藉等与产品质量的一些思考(转)
  • centOS下安装JDK
  • 网络安全系列之四十七 在IIS6中申请并安装证书
  • 使用jQuery.FileUpload和Backload自定义控制器上传多个文件
  • 从零开始iOS8编程【HelloWorld】
  • 微软职位内部推荐-SDEII for Windows Phone Apps
  • “大数据应用场景”之隔壁老王(连载四)
  • 【Leetcode】104. 二叉树的最大深度
  • 4个实用的微服务测试策略
  • Angular Elements 及其运作原理
  • extract-text-webpack-plugin用法
  • Fastjson的基本使用方法大全
  • HTTP--网络协议分层,http历史(二)
  • jQuery(一)
  • React Native移动开发实战-3-实现页面间的数据传递
  • 从地狱到天堂,Node 回调向 async/await 转变
  • 从零搭建Koa2 Server
  • 发布国内首个无服务器容器服务,运维效率从未如此高效
  • 分享一个自己写的基于canvas的原生js图片爆炸插件
  • 诡异!React stopPropagation失灵
  • 力扣(LeetCode)357
  • 前端学习笔记之原型——一张图说明`prototype`和`__proto__`的区别
  • 如何打造100亿SDK累计覆盖量的大数据系统
  • 为什么要用IPython/Jupyter?
  • 我是如何设计 Upload 上传组件的
  • 因为阿里,他们成了“杭漂”
  • 3月27日云栖精选夜读 | 从 “城市大脑”实践,瞭望未来城市源起 ...
  • 树莓派用上kodexplorer也能玩成私有网盘
  • # 安徽锐锋科技IDMS系统简介
  • #DBA杂记1
  • #QT(智能家居界面-界面切换)
  • $ git push -u origin master 推送到远程库出错
  • $().each和$.each的区别
  • ${factoryList }后面有空格不影响
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (7)STL算法之交换赋值
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (附源码)ssm码农论坛 毕业设计 231126
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (一)RocketMQ初步认识
  • (转)我也是一只IT小小鸟
  • (轉貼) VS2005 快捷键 (初級) (.NET) (Visual Studio)
  • **Java有哪些悲观锁的实现_乐观锁、悲观锁、Redis分布式锁和Zookeeper分布式锁的实现以及流程原理...
  • **PHP分步表单提交思路(分页表单提交)
  • ... fatal error LINK1120:1个无法解析的外部命令 的解决办法
  • .\OBJ\test1.axf: Error: L6230W: Ignoring --entry command. Cannot find argumen 'Reset_Handler'