ASP.NET批量下載檔案

來源:互聯網
上載者:User

標籤:style   http   io   ar   os   for   sp   strong   檔案   

一、實現步驟


  在使用者操作介面,由使用者選擇需要下載的檔案,系統根據所選檔案,在伺服器上建立用於儲存所選檔案的臨時檔案夾,將所選檔案拷貝至臨時檔案夾。然後調用 RAR程式,對臨時檔案夾進行壓縮,然後輸出到用戶端。最後刪除臨時檔案夾。

 

二、代碼實現

  1、ASP.NET批量下載 核心代碼  C# 代碼   複製
//遍曆伺服器指定檔案夾下的所有檔案 string path = "uploads/Image/"; string serverPath = Server.MapPath(path); //建立臨時檔案夾 string tempName = DateTime.Now.ToString("yyyyMMddHHMMss"); string tempFolder = Path.Combine(serverPath, tempName); Directory.CreateDirectory(tempFolder); DirectoryInfo folder = new DirectoryInfo(serverPath); foreach (FileInfo file in folder.GetFiles()) { string filename = file.Name; File.Copy(serverPath + "/" + filename, tempFolder + "/" + filename); } //ZKHelper.JSHelper.Alert("圖片拷貝成功!"); //產生RAR檔案,及檔案輸出 RARSave(tempFolder, tempName); DownloadRAR(tempFolder + "\\\\" + tempName + ".rar");

 

2、RARSave(string tempFolder, string tempName) 方法

 

 C# 代碼   複製
/// <summary> /// 產生RAR檔案 /// </summary> /// <param name="path">存放複製檔案的目錄</param> /// <param name="rarPatch">RAR檔案存放目錄</param> /// <param name="rarName">RAR檔案名稱</param> private void RARSave(string rarPatch, string rarName) { string the_rar; RegistryKey the_Reg; Object the_Obj; string the_Info; ProcessStartInfo the_StartInfo; Process the_Process; try { the_Reg = Registry.ClassesRoot.OpenSubKey(@"WinRAR"); the_Obj = the_Reg.GetValue(""); the_rar = the_Obj.ToString(); the_Reg.Close(); the_rar = the_rar.Substring(1, the_rar.Length - 7); the_Info = " a " + rarName + " -r"; the_StartInfo = new ProcessStartInfo(); the_StartInfo.FileName = "WinRar";//the_rar; the_StartInfo.Arguments = the_Info; the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //打包檔案存放目錄 the_StartInfo.WorkingDirectory = rarPatch; the_Process = new Process(); the_Process.StartInfo = the_StartInfo; the_Process.Start(); the_Process.WaitForExit(); the_Process.Close(); } catch (Exception) { throw; } }

 

3、DownloadRAR(string file)方法

 

 C# 代碼   複製
/// <summary> /// 下載產生的RAR檔案 /// </summary> private void DownloadRAR(string file) { FileInfo fileInfo = new FileInfo(file); Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name); 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(); string tempPath = file.Substring(0, file.LastIndexOf("\\\\")); //刪除臨時目錄下的所有檔案 DeleteFiles(tempPath); //刪除空目錄 Directory.Delete(tempPath); Response.End(); }


4、DeleteFiles(string tempPath) 方法

 

 C# 代碼   複製
/// <summary> /// 刪除臨時目錄下的所有檔案 /// </summary> /// <param name="tempPath">臨時目錄路徑</param> private void DeleteFiles(string tempPath) { DirectoryInfo directory = new DirectoryInfo(tempPath); try { foreach (FileInfo file in directory.GetFiles()) { if (file.Attributes.ToString().IndexOf("ReadOnly") != -1) { file.Attributes = FileAttributes.Normal; } File.Delete(file.FullName); } } catch (Exception) { throw; } }

ASP.NET批量下載檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.