Asp. NET Bulk Download File Method _ Practical skills

Source: Internet
Author: User
Tags rar

The example in this article describes the method of ASP.net bulk downloading files. Share to everyone for your reference. The specific methods are as follows:

First, the realization step

In the user interface, the user selects the files that need to be downloaded, and 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 the temporary folder. The RAR program is then invoked to compress the temporary folder and then output to the client. Finally, the temporary folder is deleted.

Second, code implementation

1, ASP. NET bulk Download core code

Copy Code code as follows:
Traverse all files under the server's specified folder
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 ("Picture copy successful!");
produce RAR file, and file output
Rarsave (TempFolder, tempname);
Downloadrar (TempFolder + " \\\\" + tempname + ". rar");


2, Rarsave (string tempfolder, String tempname) method

Copy Code code as follows:
<summary>
Generate a RAR file
</summary>
<param name= "path" > directory for copying Files </param>
<param name= "Rarpatch" >rar file storage directory </param>
<param name= "rarname" >rar filename </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;
Package File storage 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
}
}


3. Downloadrar (String file) method

Copy Code code as follows:
///<summary>
///Download the generated RAR file
///</summary>
private void Downloadrar (string file)
{
&N bsp;   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 temp directory
Deletefiles (TempPath);
Delete Empty Directory
Directory.delete (TempPath);
Response.End ();
}

4. Deletefiles (String TempPath) method

Copy Code code as follows:
<summary>
Delete all files in the temp directory
</summary>
<param name= "TempPath" > Temp 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 with the asp.net# program design.

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.