婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > ASP.NET批量下載文件的方法

ASP.NET批量下載文件的方法

熱門標簽:浦發電話機器人提醒還款 電銷語音機器人型號參數 太原400電話上門辦理 昆明語音電銷機器人價格 柳州電銷機器人公司 百度地圖怎樣做地圖標注 騰訊地圖標注手機 400電話如何申請取消 征途美甲店地圖標注

本文實例講述了ASP.NET批量下載文件的方法。分享給大家供大家參考。具體方法如下:

一、實現步驟

在用戶操作界面,由用戶選擇需要下載的文件,系統根據所選文件,在服務器上創建用于存儲所選文件的臨時文件夾,將所選文件拷貝至臨時文件夾。然后調用 RAR程序,對臨時文件夾進行壓縮,然后輸出到客戶端。最后刪除臨時文件夾。
 
二、代碼實現
 
1、ASP.NET批量下載 核心代碼

復制代碼 代碼如下:
//遍歷服務器指定文件夾下的所有文件
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) 方法

復制代碼 代碼如下:
/// 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)方法

復制代碼 代碼如下:
/// 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) 方法

復制代碼 代碼如下:
/// 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#程序設計有所幫助。

您可能感興趣的文章:
  • ASP.NET Web Api 2實現多文件打包并下載文件的實例
  • ASP.NET(C#) Web Api通過文件流下載文件的實例
  • ASP.NET 在下載文件時對其重命名的思路及實現方法
  • asp.net C#實現下載文件的六種方法實例
  • ASP.NET中下載文件的幾種實例代碼
  • 在ASP.NET中下載文件的實現代碼
  • asp.net BackgroundWorker之在后臺下載文件
  • asp.net 下載文件時根據MIME類型自動判斷保存文件的擴展名
  • asp.net 下載文件時輸出文件內容
  • asp.net Web Services上傳和下載文件(完整代碼)
  • ASP.NET實現從服務器下載文件問題處理

標簽:新疆 德陽 天門 白山 江蘇 陽泉 張家界 蘭州

巨人網絡通訊聲明:本文標題《ASP.NET批量下載文件的方法》,本文關鍵詞  ASP.NET,批量,下載,文件,的,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《ASP.NET批量下載文件的方法》相關的同類信息!
  • 本頁收集關于ASP.NET批量下載文件的方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 开远市| 祁东县| 霍州市| 临桂县| 化隆| 新竹县| 松原市| 仲巴县| 历史| 灵丘县| 岱山县| 福安市| 绵阳市| 北海市| 嘉鱼县| 琼结县| 彭山县| 永城市| 太谷县| 海阳市| 龙游县| 封开县| 桑日县| 福海县| 浦北县| 荥阳市| 航空| 四平市| 宣汉县| 沾化县| 澄迈县| 木兰县| 萨嘎县| 塘沽区| 临安市| 兴义市| 通海县| 鄂托克前旗| 商洛市| 盐边县| 昌平区|