欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2014年11月07日 09:50:11   投稿:shichen2014  
這篇文章主要介紹了ASP.NET批量下載文件的方法,實例匯總了常見的asp.net實現(xiàn)批量下載的方法,具有一定的實用價值,需要的朋友可以參考下

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

一、實現(xiàn)步驟

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

復(fù)制代碼 代碼如下:
//遍歷服務(wù)器指定文件夾下的所有文件
string path = "uploads/Image/";
string serverPath = Server.MapPath(path);
//創(chuàng)建臨時文件夾
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("\\\\"));
    //刪除臨時目錄下的所有文件
    DeleteFiles(tempPath);
    //刪除空目錄
    Directory.Delete(tempPath);
    Response.End();
}

4、DeleteFiles(string tempPath) 方法

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

相關(guān)文章

最新評論