ASP.NETファイルの一括ダウンロード方法

4507 ワード

この例では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)方法

   ///  
  
/// RAR
///

///
/// RAR
/// RAR
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)方法

   ///  
  
/// RAR
///

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)方法

   ///  
  
///
///

///
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#プログラム設計が役立ちます.