场景描述
在B/S环境下客户提出批量导出员工照片功能具体为选中一个部门或者单位系统能够批量下载所选单元的照片下载到用户客户端
解决思路
由于系统中员工的照片存储在服务器硬盘上因此应该有两种方式供用户选择其一写一个C/S客户端利用客户端功能实现客户端批量下载操作其二在现有ASPNET环境下将所需照片文件合并成一个文件下载到用户客户端比较而言两种思路的难度都不大但是考虑到系统的统一性最终决定采用方案二将文件打包后下载
实现步骤
在用户操作界面由用户选择员工系统根据所选人员在服务器上创建用于存储所选文件的临时文件夹将所选文件拷贝至临时文件夹然后调用RAR程序对临时文件夹进行压缩然后输出到客户端最后删除临时文件夹
部分关键代码
创建临时文件夹
string Folder = DateTimeNowToString(HHMMss);
string tempFolder = PathCombine(ImagesPath Folder);
DirectoryCreateDirectory(tempFolder);
var empList = rsToList();
拷贝照片文件
foreach (var x in empList)
{
FileCopy(ImagesPath + @\ + xID + jpg tempFolder + @\ + xDeptName + + xName + + xID + 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 = RegistryClassesRootOpenSubKey(@WinRAR);
the_Obj = the_RegGetValue();
the_rar = the_ObjToString();
the_RegClose();
the_rar = the_rarSubstring( the_rarLength );
DirectoryCreateDirectory(patch);
//命令参数
//the_Info = a + rarName + + @C:Test?txt; //文件压缩
the_Info = a + rarName + + patch + r;
the_StartInfo = new ProcessStartInfo();
the_StartInfoFileName = WinRar;//the_rar;
the_StartInfoArguments = the_Info;
the_StartInfoWindowStyle = ProcessWindowStyleHidden;
//打包文件存放目录
the_StartInfoWorkingDirectory = rarPatch;
the_Process = new Process();
the_ProcessStartInfo = the_StartInfo;
the_ProcessStart();
the_ProcessWaitForExit();
the_ProcessClose();
}
catch (Exception ex)
{
throw ex;
}
}
protected void ResponseFile(string fileName)
{
FileInfo fileInfo = new FileInfo(fileName);
ResponseClear();
ResponseClearContent();
ResponseClearHeaders();
ResponseAddHeader(ContentDisposition attachment;filename= + fileName);
ResponseAddHeader(ContentLength fileInfoLengthToString());
ResponseAddHeader(ContentTransferEncoding binary);
ResponseContentType = application/octetstream;
ResponseContentEncoding = SystemTextEncodingGetEncoding(gb);
ResponseWriteFile(fileInfoFullName);
ResponseFlush();
string tempPath = fileNameSubstring( fileNameLastIndexOf(\\));
DelDir(tempPath);
DirectoryDelete(tempPath);
ResponseEnd();
}
文章出处/jizhong