Asp.net enables simultaneous download of multiple files, and asp.net supports multiple
The example in this article shares the specific code for simultaneous downloading of multiple asp.net files for your reference. The specific content is as follows:
1. First, read the files in the folder. Multiple files may exist at the same time.
2. Select a file, click Download, and select multiple files.
Ideas:Download the package in the form of a production package, and then clear the package, so that you can download it all at once.
1. Obtain all files in the directory and bind them to the checkboxlist. The Code is as follows:
Ckl_ck.Items.Clear (); DirectoryInfo TheFolder = new DirectoryInfo (Server. mapPath ("Resource/Help"); // traverses the file foreach (FileInfo NextFile in TheFolder. getFiles () this. ckl_ck.Items.Add (NextFile. name );
2. Select a file and click Download. Code:
Protected void Btn_down_Click (object sender, EventArgs e) {if (ckl_ck.Items.Count> 0) {List <string> listFJ = new List <string> (); // Save the attachment path List <string> listFJName = new List <string> (); // Save the attachment name for (int I = 0; I <ckl_ck.Items.Count; I ++) {if (ckl_ck.Items [I]. selected) {listFJ. add (Server. mapPath ("Resource/Help/") + ckl_ck.Items [I]. text); listFJName. add (ckl_ck.Items [I]. text) ;}} string time = DateTime. now. ticks. toString (); ZipFileMain (listFJ. toArray (), listFJName. toArray (), Server. mapPath ("Resource/Help/" + time + ". zip "), 9); // compressed file DownloadFile (Server. urlEncode ("attachment .zip"), Server. mapPath ("Resource/Help/" + time + ". zip "); // download file} private void DownloadFile (string fileName, string filePath) {FileInfo fileInfo = new FileInfo (filePath); 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 (); File. delete (filePath); // Delete the downloaded file Response. end ();} /// <summary> /// compressed file /// </summary> /// <param name = "fileName"> all files to be compressed (full path) </param> /// <param name = "fileName"> file name </param> /// <param name = "name"> compressed file path </param> /// <param name = "Level"> compression Level </param> public void ZipFileMain (string [] filenames, string [] fileName, string name, int Level) {ZipOutputStream s = new ZipOutputStream (File. create (name); Crc32 crc = new Crc32 (); // compression level s. setLevel (Level); // 0-store only to 9-means best compression try {int m = 0; foreach (string file in filenames) {// open the compressed File FileStream fs = File. openRead (file); // file address byte [] buffer = new byte [fs. length]; fs. read (buffer, 0, buffer. length); // create a compressed object ZipEntry entry = new ZipEntry (fileName [m]. toString (); // original file name // time entry. dateTime = DateTime. now; // space size entry. size = fs. length; fs. close (); crc. reset (); crc. update (buffer); entry. crc = crc. value; s. putNextEntry (entry); s. write (buffer, 0, buffer. length); m ++ ;}} catch {throw;} finally {s. finish (); s. close ();}}
3. Download the dll to be referenced in the system.
Iv. Running Effect
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.