First, the implementation of steps
In the user interface, the user selects the files that need to be downloaded, the system creates a temporary folder on the server to store the selected files based on the selected files, and copies the selected files to a temporary folder. Then the RAR program is called, the Temp folder is compressed, and then output to the client. Finally, delete the Temp folder.
Second, the Code implementation
1, ASP. NET bulk Download core codeC # codeCopy
//Traverse all files under the specified folder of the server StringPath= "uploads/image/";StringServerpath=Server.MapPath (path);//Create a temporary folder StringTempname=DateTime.Now.ToString ("Yyyymmddhhmmss");StringTempFolder=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 ("Picture copy succeeded!"); // generate RAR file, and file output rarsave (TempFolder, tempname); Downloadrar (tempfolder + " \\\\ " + tempname + " . rar ");
2, Rarsave (string tempfolder, String tempname) method
C # codeCopy
/// <summary> ///Generate RAR File/// </summary> /// <param name= "Path" >directory where the copied files are stored</param> /// <param name= "Rarpatch" >RAR File storage Directory</param> /// <param name= "Rarname" >RAR file name</param> Private voidRarsave (StringRarpatch,StringRarname){StringThe_rar; RegistryKey The_reg; Object The_obj;StringThe_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; // Packaging Files directory 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 ;} } /span>
3. Downloadrar (string file) method
C # codeCopy
/// <summary> ///Download the generated RAR file/// </summary> Private voidDownloadrar (StringFile{FileInfo FileInfo= NewFileInfo (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 ( " \\\\ " )); // Delete all files under the temp directory deletefiles (TempPath); // Delete empty directory directory.delete (TempPath); Response.End (); }
4, Deletefiles (string temppath) method
C # codeCopy
/// <summary> ///Delete all files in temp directory/// </summary> /// <param name= "TempPath" >Temp directory path</param> Private voidDeletefiles (StringTempPath) {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 ;} } /span>
ASP. NET bulk Download file