This topic explains how to download ASP. NET files in batches.
1. Scenario Description
In the B/S environment, the customer proposes the function of exporting Employee photos in batches. Specifically: select a department or unit. The system can batch download photos of the selected units and download them to the user client.
2. Solution
Because the photos of employees in the system are stored on the server hard disk, there are two ways for users to choose from: First, write a C/S client and use the client function, batch download on the client. Second, in the existing ASP. NET environment, combine the required photo files into a file and download it to the user client. In comparison, the two ideas are not difficult, but considering the uniformity of the system, the final decision is to adopt solution 2, package the file and download it.
3. Implementation steps
On the user interface, select an employee. The system creates a temporary folder on the server to store the selected files based on the selected personnel and copies the selected files to the Temporary Folder. Then, the RAR program is called to compress the Temporary Folder and output it to the client. Finally, delete the Temporary Folder.
4. Some key code
Create Temporary Folder
String Folder = DateTime. Now. ToString ("HHMMss ");
String tempFolder = Path. Combine (ImagesPath, Folder );
Directory. CreateDirectory (tempFolder );
Var empList = rs. ToList ();
Copy photo files
Foreach (var x in empList)
{
File. copy (ImagesPath @ "" x. ID ". jpg ", tempFolder @" "x. deptName "-" x. name "-" x. ID ". jpg ");
}
Generate rarfile and file output
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 );
// Command Parameters
// The_Info = "a" rarName "" @ "C: Test? 70821. txt "; // File compression
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;
// Directory for storing packaged files
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 ();
}