ASP. NET batch download files, asp.net download files

Source: Internet
Author: User

ASP. NET batch download files, asp.net download files

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. NETC # code Replication
// 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 (serv ErPath + "/" + filename, tempFolder + "/" + filename);} // ZKHelper. JSHelper. Alert ("image copied successfully! "); // Generate the rarfile and output the RARSave (tempFolder, tempName); DownloadRAR (tempFolder +" \\\\ "+ tempName +". rar ");

 

2. RARSave (string tempFolder, string tempName) Method

 

C # code Replication
/// <Summary> /// generate the rarfile /// </summary> /// <param name = "path"> directory for storing 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 = trim (1, the_rar.Length-7 ); the_Info = "a" + rarName + "-r"; the_StartInfo = new ProcessStartInfo (); plaintext = "WinRar"; // the_rar; plaintext = the_Info; the_StartInfo.WindowStyle = ProcessWindowStyle. hidden; // directory where the packaged files are stored; kernel = rarPatch; the_Process = new Process (); kernel = the_StartInfo; the_Process.Start (); the_Process.WaitForExit (); the_Process.Close ();} catch (Exception) {throw ;}}

 

3. DownloadRAR (string file) Method

 

C # code Replication
/// <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 the empty Directory. delete (tempPath); Response. end ();}


4. DeleteFiles (string tempPath) Method

 

C # code Replication
/// <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 ;}}

How can I package and download multiple files in aspnet?

First download ICSharpCode. SharpZipLib. dll
Public string cutStr = "";
Public void ZipFile (string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
{
// If the file is not found, an error is returned.
If (! System. IO. File. Exists (FileToZip ))
{
Throw new System. IO. FileNotFoundException ("The specified file" + FileToZip + "cocould not be found. Zipping aborderd ");
}
System. IO. FileStream StreamToZip = new System. IO. FileStream (FileToZip, System. IO. FileMode. Open, System. IO. FileAccess. Read );
System. IO. FileStream ZipFile = System. IO. File. Create (ZipedFile );
ZipOutputStream ZipStream = new ZipOutputStream (ZipFile );
ZipEntry = new ZipEntry ("ZippedFile ");
ZipStream. PutNextEntry (ZipEntry );
ZipStream. SetLevel (CompressionLevel );
Byte [] buffer = new byte [BlockSize];
System. Int32 size = StreamToZip. Read (buffer, 0, buffer. Length );
ZipStream. Write (buffer, 0, size );
Try
{
While (size <StreamToZip. Length)
{
Int sizeRead = StreamToZip. Read (buffer, 0, buffer. Length );
ZipStream. Write (buffer, 0, sizeRead );
Size + = sizeRead;
}
}
Catch (System. Exception ex)
{
Throw ex;
}
ZipStream. Finish ();
ZipStream. Close ();
StreamToZip. Close ();
}
// Get all DirectoryInfo
Private void direct (DirectoryInfo di, ref ZipOutputStream s, Crc32 crc)
{
// Direc... the remaining full text>

Example of Batch File Download In aspnet

// The constructor does not need to return the type, which is public Employee (string name, int age)
Public Employee (string name, int age) // you have multiple voids in your code.
{
This. Name = name;
This. Age = age;
This. zy = "Occupation ";
}

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.