ASP.NET下文件批量下载利用[网站编程]
本文“ASP.NET下文件批量下载利用[网站编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
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下文件批量下载利用[网站编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |