This article mainly introduces the function of asp.net to download multiple files at the same time in detail. It has certain reference value. Interested friends can refer to it.
The examples in this article are shared with you. The specific code for downloading multiple files at the same time in asp.net is 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 the file and click Download. You can select multiple files at the same time.
Idea:Download by producing a 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")); //遍历文件夹下的文件 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>();//保存附件路径 List<string> listFJName = new List<string>();//保存附件名字 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);//压缩文件 DownloadFile(Server.UrlEncode("附件.zip"), Server.MapPath("Resource/Help/" + time + ".zip"));//下载文件 } } 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);//删除已下载文件 Response.End(); } /// <summary> /// 压缩文件 /// </summary> /// <param name="fileName">要压缩的所有文件(完全路径)</param> /// <param name="fileName">文件名称</param> /// <param name="name">压缩后文件路径</param> /// <param name="Level">压缩级别</param> public void ZipFileMain(string[] filenames, string[] fileName, string name, int Level) { ZipOutputStream s = new ZipOutputStream(File.Create(name)); Crc32 crc = new Crc32(); //压缩级别 s.SetLevel(Level); // 0 - store only to 9 - means best compression try { int m = 0; foreach (string file in filenames) { //打开压缩文件 FileStream fs = File.OpenRead(file);//文件地址 byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); //建立压缩实体 ZipEntry entry = new ZipEntry(fileName[m].ToString());//原文件名 //时间 entry.DateTime = DateTime.Now; //空间大小 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. The dll that needs to be referenced in the system needs to be downloaded.
4. The running effect is as shown in the figure:
The above is the detailed content of Share an asp.net method to download multiple files at the same time. For more information, please follow other related articles on the PHP Chinese website!