ASP. NET batch file download method, asp.net Batch File Download Method

Source: Internet
Author: User

ASP. NET batch file download method, asp.net Batch File Download Method

This document describes how to download files in batches using ASP. NET. Share it with you for your reference. The specific method is as follows:

1. Implementation steps

On the user interface, select the file to be downloaded. Based on the selected file, the system creates a temporary folder on the server to store the selected file and copies the selected file 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.
 
Ii. Code Implementation
 
1. Download core code in batches using ASP. NET

Copy codeThe Code is as follows: // traverse all files in the specified folder on the server
String path = "uploads/Image /";
String serverPath = Server. MapPath (path );
// Create a Temporary Folder
String tempName = DateTime. Now. ToString ("yyyyMMddHHMMss ");
String tempFolder = 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 ("image copied successfully! ");
// Generate the rarfile and file output
RARSave (tempFolder, tempName );
DownloadRAR (tempFolder + "\\\\" + tempName + ". rar ");
 
2. RARSave (string tempFolder, string tempName) Method

Copy codeThe Code is as follows: // <summary>
/// Generate the rarfile
/// </Summary>
/// <Param name = "path"> directory of the copied file </param>
/// <Param name = "rarPatch"> rarfile storage directory </param>
/// <Param name = "rarName"> RAR file name </param>
Private void RARSave (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 );
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;
// 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)
{
Throw;
}
}
 
3. DownloadRAR (string file) Method

Copy codeThe Code is as follows: // <summary>
/// Download the generated rarfile
/// </Summary>
Private void DownloadRAR (string file)
{
FileInfo fileInfo = new FileInfo (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 in the temporary directory
DeleteFiles (tempPath );
// Delete an empty directory
Directory. Delete (tempPath );
Response. End ();
}

4. DeleteFiles (string tempPath) Method

Copy codeThe Code is as follows: // <summary>
/// Delete all files in the temporary directory
/// </Summary>
/// <Param name = "tempPath"> temporary directory path </param>
Private void DeleteFiles (string tempPath)
{
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;
}
}

I hope this article will help you design your asp.net # program.


C # How does aspnet implement batch download?

Download the package,
No score, no code
 
How does aspnet package all files in the folder and prompt to download and save

This is quite simple. First of all:
1. Get all files in the folder: obtain all files under the folder using the Dictionary. GetFiles () method ()
2. package according to the obtained file set: ICSharpCode. SharpZipLib. Zip is recommended for File compression and decompression. This is comprehensive.
3. as for downloading and saving, it is simpler. You only need to store the generated file path in the database at step 1, and then bind the path to the page; then the user will click the link, and the browser will automatically pop up a prompt indicating to download and save.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.