For more information about how to implement multiple files at the same time download problems related answers

Source: Internet
Author: User
Tags crc32
This article is mainly for you to introduce in detail the ASP. NET implementation of multiple files at the same time the download function, with a certain reference value, interested in small partners can refer to

The example of this article for everyone to share the ASP. NET multiple files downloaded at the same time the specific code for your reference, the specific content as follows

1, first read the file under the folder, there may be multiple files at the same time

2. Select the file and click Download, and you can select multiple files.

idea: download it in the form of a compressed package, then clear the package so that the user can download it all at once.

First, get all the files in the directory, and then bind to CheckBoxList, the code is as follows:

Ckl_ck. Items.clear (); DirectoryInfo Thefolder = new DirectoryInfo (Server.MapPath ("Resource/help")); Traverse the file under the folder foreach (FileInfo nextfile in Thefolder.getfiles ())  This.ckl_ck. Items.Add (Nextfile.name);

Second, select the file, click the Download button. Code:

 protected void Btn_down_click (object sender, EventArgs e) {if (ckl_ck. Items.Count > 0) {list<string> listfj = new list<string> ();//Save attachment path list<string> Listfjname = new List<string> ();//Save 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 downloaded file Response.End (); }//<summary>///Compressed File///</summary>//<param name= "FileName" > All files to be compressed (full path) </param>//&lt ;p Aram Name= "fileName" > File name </param>//<param name= "name" > Compressed file path </param>//<param name= " Level > Compression levels </param> public void Zipfilemain (string[) filenames, string[] fileName, string name, int level) {Zipo Utputstream s = new Zipoutputstream (file.create (name)); Crc32 CRC = New Crc32 (); Compression levels S.setlevel (level);  0-store-9-means Best compression try {int m = 0; foreach (string file in filenames) {//Open compressed file FileStream fs = file.openread (file);//filename address byte[] buffer = new BYTE[FS.L  Ength]; Fs. Read (buffer, 0, buffer.  Length); Establish a compactionBody ZipEntry entry = new ZipEntry (Filename[m]. ToString ());//the original filename//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 (); } }

Third, the system needs to refer to the DLL needs to download.

Four, the Operation effect

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.