IT猫扑网:您身边最放心的安全下载站! 最新更新| 软件分类| 专题汇总| 手机版

您当前所在位置:IT猫扑网 > 操作系统 > LINUX > ASP.NET下文件批量下载应用

ASP.NET下文件批量下载应用

时间:2015-06-28 00:00 来源:IT猫扑网|http://www.itmop.com/ 作者:网管联盟 我要评论(0)

  1.场景描述

  在B/S环境下,客户提出批量导出员工照片功能。具体为:选中一个部门或者单位,系统能够批量下载所选单元的照片,下载到用户客户端。

  2.解决思路

  由于系统中员工的照片存储在服务器硬盘上,因此,应该有两种方式供用户选择:其一,写一个C/S客户端,利用客户端功能,实现客户端批量下载操作。其二,在现有ASP.NET环境下,将所需照片文件合并成一个文件下载到用户客户端。比较而言,两种思路的难度都不大,但是考虑到系统的统一性,最终决定采用方案二,将文件打包后下载。

  3.实现步骤

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

  4.部分关键代码

  创建临时文件夹

  string Folder = DateTime.Now.ToString(&HHMMss&);

  string tempFolder = Path.Combine(ImagesPath, Folder);

  Directory.CreateDirectory(tempFolder);

  var empList = rs.ToList();

  拷贝照片文件

  foreach (var x in empList)

  {

  File.Copy(ImagesPath + @&& + x.ID + &.jpg&, tempFolder + @&& + x.DeptName + &-& + x.Name + &-& + x.ID + &.jpg&);

  }

  产生RAR文件,及文件输出

  RARsave(tempFolder, tempFolder, Folder);

  ResponseFile(tempFolder + @&& + Folder + &.rar&);

  public void RARsave(string patch, 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);

  Directory.CreateDirectory(patch);

  //命令参数

  //the_Info = & a    & + rarName + &  & + @&C:Test?70821.txt&; //文件压缩

  the_Info = & a & + rarName + &  & + patch + &  -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 ex)

  {

  throw ex;

  }

  }

  protected void ResponseFile(string fileName)

  {

  FileInfo fileInfo = new FileInfo(fileName);

  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();

  string tempPath = fileName.Substring(0, fileName.LastIndexOf(&\&));

  DelDir(tempPath);

  Directory.Delete(tempPath);

  Response.End();

  }

关键词标签:ASP.NET

相关阅读 安装红帽子RedHat Linux9.0操作系统教程 Tomcat9.0如何安装_Tomcat9.0环境变量配置方法 多种操作系统NTP客户端配置 Linux操作系统修改IP Linux实现SCSI硬盘热插拔及在线识别 Linux下用CDMA modem拨号上网

文章评论
发表评论

热门文章 安装红帽子RedHat Linux9.0操作系统教程 安装红帽子RedHat Linux9.0操作系统教程 Linux服务器:设计高性能网站架构-LLMP Linux服务器:设计高性能网站架构-LLMP 使用Clonezilla迁移到虚拟Linux环境 使用Clonezilla迁移到虚拟Linux环境 Linux上的MRTG流量监控中心 Linux上的MRTG流量监控中心 Linux 双网卡绑定一个IP原理及实现 Linux 双网卡绑定一个IP原理及实现 linux和windows等系统远程控制ubuntu桌面 linux和windows等系统远程控制ubuntu桌面

相关下载

人气排行 Linux下获取CPUID、硬盘序列号与MAC地址 dmidecode命令查看内存型号 linux tc实现ip流量限制 安装红帽子RedHat Linux9.0操作系统教程 linux下解压rar文件 lcx.exe、nc.exe、sc.exe入侵中的使用方法 Ubuntu linux 关机、重启、注销 命令 查看linux服务器硬盘IO读写负载 linux命令行浏览器的使用方法 Linux NFS服务固定端口及防火墙配置 U盘安装Ubuntu 10.04 Linux清除用户登录记录和命令历史方法