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

主頁(yè) > 知識(shí)庫(kù) > ASP.NET批量下載文件的方法

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

熱門標(biāo)簽:浦發(fā)電話機(jī)器人提醒還款 電銷語(yǔ)音機(jī)器人型號(hào)參數(shù) 太原400電話上門辦理 昆明語(yǔ)音電銷機(jī)器人價(jià)格 柳州電銷機(jī)器人公司 百度地圖怎樣做地圖標(biāo)注 騰訊地圖標(biāo)注手機(jī) 400電話如何申請(qǐng)取消 征途美甲店地圖標(biāo)注

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

一、實(shí)現(xiàn)步驟

在用戶操作界面,由用戶選擇需要下載的文件,系統(tǒng)根據(jù)所選文件,在服務(wù)器上創(chuàng)建用于存儲(chǔ)所選文件的臨時(shí)文件夾,將所選文件拷貝至臨時(shí)文件夾。然后調(diào)用 RAR程序,對(duì)臨時(shí)文件夾進(jìn)行壓縮,然后輸出到客戶端。最后刪除臨時(shí)文件夾。
 
二、代碼實(shí)現(xiàn)
 
1、ASP.NET批量下載 核心代碼

復(fù)制代碼 代碼如下:
//遍歷服務(wù)器指定文件夾下的所有文件
string path = "uploads/Image/";
string serverPath = Server.MapPath(path);
//創(chuàng)建臨時(shí)文件夾
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("圖片拷貝成功!");
//產(chǎn)生RAR文件,及文件輸出
RARSave(tempFolder, tempName);
DownloadRAR(tempFolder + "\\\\" + tempName + ".rar");

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

復(fù)制代碼 代碼如下:
/// summary>
/// 生成RAR文件
/// /summary>
/// param name="path">存放復(fù)制文件的目錄/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)方法

復(fù)制代碼 代碼如下:
/// 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("\\\\"));
    //刪除臨時(shí)目錄下的所有文件
    DeleteFiles(tempPath);
    //刪除空目錄
    Directory.Delete(tempPath);
    Response.End();
}

4、DeleteFiles(string tempPath) 方法

復(fù)制代碼 代碼如下:
/// summary>
/// 刪除臨時(shí)目錄下的所有文件
/// /summary>
/// param name="tempPath">臨時(shí)目錄路徑/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;
    }
}

希望本文所述對(duì)大家的asp.net#程序設(shè)計(jì)有所幫助。

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

標(biāo)簽:新疆 德陽(yáng) 天門 白山 江蘇 陽(yáng)泉 張家界 蘭州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.NET批量下載文件的方法》,本文關(guān)鍵詞  ASP.NET,批量,下載,文件,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《ASP.NET批量下載文件的方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于ASP.NET批量下載文件的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 台东县| 东丰县| 苗栗县| 长海县| 泽普县| 醴陵市| 河池市| 扎鲁特旗| 乡宁县| 溆浦县| 铜梁县| 鸡泽县| 乌兰浩特市| 泰兴市| 莱芜市| 嵊州市| 石城县| 阳信县| 闸北区| 广昌县| 静乐县| 通辽市| 桃园市| 石渠县| 东安县| 息烽县| 通河县| 柳林县| 东阿县| 临城县| 马鞍山市| 神木县| 牡丹江市| 临城县| 武宣县| 宣恩县| 台中县| 辛集市| 革吉县| 青神县| 桂东县|