Batch download of files under ASP. NET

Source: Internet
Author: User
Batch download of files under ASP. NET

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, and the system can batch download photos of the selected unit to the 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 = 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 ();

}

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.