ASP.NET環境下檔案批量下載及應用

來源:互聯網
上載者:User

1.情境描述

在B/S環境下,客戶提出大量匯出員工照片功能。具體為:選中一個部門或者單位,系統能夠批量下載所選單元的照片到用戶端。

2.解決思路

由於系統中員工的照片儲存在伺服器硬碟上,因此,應該有兩種方式供使用者選擇:其一,寫一個C/S用戶端,利用用戶端功能,實現用戶端批量下載操作。其二,在現有ASP.NET環境下,將所需照片檔案合并成一個檔案下載到使用者用戶端。比較而言,兩種思路的難度都不大,但是考慮到系統的統一性,最終決定採用方案二,將檔案打包後下載。

3.實現步驟

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

4.部分關鍵代碼

建立臨時檔案夾

string Folder = DateTime.Now.ToString("HHMMss");

string tempFolder = Path.Combine(ImagesPath, Folder);

Directory.CreateDirectory(tempFolder);

var empList = rs.ToList();

 

拷貝照片檔案

 

foreach (var x in empList)
            {
                File.Copy(ImagesPath + @"\" + x.ID + ".jpg", tempFolder + @"\" + x.DeptName + "-" + x.Name + "-" + x.ID + ".jpg");
            }

 

產生RAR檔案,及檔案輸出

 

RARsave(tempFolder, tempFolder, Folder);
ResponseFile(tempFolder + @"\" + Folder + ".rar"); public void RARsave(string patch, 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);
            Directory.CreateDirectory(patch);
            //命令參數
            //the_Info = " a    " + rarName + "  " + @"C:Test?70821.txt"; //檔案壓縮
            the_Info = " a " + rarName + "  " + patch + "  -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 ex)
        {
            throw ex;
        }
    }

    protected void ResponseFile(string fileName)
    {

        FileInfo fileInfo = new FileInfo(fileName);
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
        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 = fileName.Substring(0, fileName.LastIndexOf("\\"));
        DelDir(tempPath);
        Directory.Delete(tempPath);
        Response.End();

    }

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.