熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET下文件批量下載應用

2022-06-13   來源: .NET編程 

  場景描述

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

  解決思路

  由於系統中員工的照片存儲在服務器硬盤上因此應該有兩種方式供用戶選擇其一寫一個C/S客戶端利用客戶端功能實現客戶端批量下載操作其二在現有ASPNET環境下將所需照片文件合並成一個文件下載到用戶客戶端比較而言兩種思路的難度都不大但是考慮到系統的統一性最終決定采用方案二將文件打包後下載

  實現步驟

  在用戶操作界面由用戶選擇員工系統根據所選人員在服務器上創建用於存儲所選文件的臨時文件夾將所選文件拷貝至臨時文件夾然後調用RAR程序對臨時文件夾進行壓縮然後輸出到客戶端最後刪除臨時文件夾

  部分關鍵代碼

  創建臨時文件夾

  string Folder = DateTimeNowToString(HHMMss);

  string tempFolder = PathCombine(ImagesPath Folder);

  DirectoryCreateDirectory(tempFolder);

  var empList = rsToList();

  拷貝照片文件

  foreach (var x in empList)

  {

  FileCopy(ImagesPath + @\ + xID + jpg tempFolder + @\ + xDeptName + + xName + + xID + 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 = RegistryClassesRootOpenSubKey(@WinRAR);

  the_Obj = the_RegGetValue();

  the_rar = the_ObjToString();

  the_RegClose();

  the_rar = the_rarSubstring( the_rarLength );

  DirectoryCreateDirectory(patch);

  //命令參數

  //the_Info = a    + rarName +   + @C:Test?txt; //文件壓縮

  the_Info = a + rarName +   + patch +   r;

  the_StartInfo = new ProcessStartInfo();

  the_StartInfoFileName = WinRar;//the_rar;

  the_StartInfoArguments = the_Info;

  the_StartInfoWindowStyle = ProcessWindowStyleHidden;

  //打包文件存放目錄

  the_StartInfoWorkingDirectory = rarPatch;

  the_Process = new Process();

  the_ProcessStartInfo = the_StartInfo;

  the_ProcessStart();

  the_ProcessWaitForExit();

  the_ProcessClose();

  }

  catch (Exception ex)

  {

  throw ex;

  }

  }

  protected void ResponseFile(string fileName)

  {

  FileInfo fileInfo = new FileInfo(fileName);

  ResponseClear();

  ResponseClearContent();

  ResponseClearHeaders();

  ResponseAddHeader(ContentDisposition attachment;filename= + fileName);

  ResponseAddHeader(ContentLength fileInfoLengthToString());

  ResponseAddHeader(ContentTransferEncoding binary);

  ResponseContentType = application/octetstream;

  ResponseContentEncoding = SystemTextEncodingGetEncoding(gb);

  ResponseWriteFile(fileInfoFullName);

  ResponseFlush();

  string tempPath = fileNameSubstring( fileNameLastIndexOf(\\));

  DelDir(tempPath);

  DirectoryDelete(tempPath);

  ResponseEnd();

  }

  文章出處/jizhong


From:http://tw.wingwit.com/Article/program/net/201311/13948.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.