Asp.net realizes the function of downloading multiple files at the same time

Time:2021-11-29

This example shares the specific code downloaded from multiple asp.net files at the same time for your reference. The specific contents are as follows

1. First read the files under the folder. There may be multiple files at the same time

2. Select a file and click download. You can select multiple files at the same time.

Idea:Download in the form of production compressed package, and then clear the compressed package, so that users can download it all at once.

1、 Get all the 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"));
 //Traverse the files under the folder
 foreach (FileInfo NextFile in TheFolder.GetFiles())
  this.ckl_ck.Items.Add(NextFile.Name);

2、 After selecting 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 files
 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 compressed file
  FileStream fs = File.OpenRead(file);// File address
  byte[] buffer = new byte[fs.Length];
  fs.Read(buffer, 0, buffer.Length);
  //Create compressed entity
  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、 DLL to be referenced in the systemdownload

4、 The operation effect is shown in the figure below:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.