溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

asp.net實現(xiàn)多個文件同時下載的方法是什么

發(fā)布時間:2020-10-16 15:02:16 來源:億速云 閱讀:130 作者:小新 欄目:編程語言

小編給大家分享一下asp.net實現(xiàn)多個文件同時下載的方法是什么,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

具體內(nèi)容如下

1、首先讀取文件夾下的文件,可能同時存在多個文件

2、選中文件,然后點擊下載,同時可以選擇多個文件。

思路:通過生產(chǎn)壓縮包的形式進行下載,然后再清楚壓縮包,這樣用戶可以一次性全部下載下來。

一、獲取目錄下的所有文件,然后綁定到checkboxlist中 ,代碼如下:

 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);

二、選中文件后,點擊下載按鈕。代碼:

 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();
 }
 }

三、系統(tǒng)中需要引用的dll 需要下載。

四、運行效果如圖:

asp.net實現(xiàn)多個文件同時下載的方法是什么

看完了這篇文章,相信你對asp.net實現(xiàn)多個文件同時下載的方法是什么有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI