ASP.NET批量下载文件

时间:2023-03-08 17:28:10
ASP.NET批量下载文件

一、实现步骤

  在用户操作界面,由用户选择需要下载的文件,系统根据所选文件,在服务器上创建用于存储所选文件的临时文件夹,将所选文件拷贝至临时文件夹。然后调用 RAR程序,对临时文件夹进行压缩,然后输出到客户端。最后删除临时文件夹。

二、代码实现

1、ASP.NET批量下载 核心代码
C# 代码   复制
ASP.NET批量下载文件
ASP.NET批量下载文件//遍历服务器指定文件夹下的所有文件
ASP.NET批量下载文件 string path = "uploads/Image/";
ASP.NET批量下载文件 string serverPath = Server.MapPath(path);
ASP.NET批量下载文件
ASP.NET批量下载文件 //创建临时文件夹
ASP.NET批量下载文件 string tempName = DateTime.Now.ToString("yyyyMMddHHMMss");
ASP.NET批量下载文件 string tempFolder = Path.Combine(serverPath, tempName);
ASP.NET批量下载文件 Directory.CreateDirectory(tempFolder);
ASP.NET批量下载文件
ASP.NET批量下载文件 DirectoryInfo folder = new DirectoryInfo(serverPath);
ASP.NET批量下载文件 foreach (FileInfo file in folder.GetFiles())
ASP.NET批量下载文件 {
ASP.NET批量下载文件 string filename = file.Name;
ASP.NET批量下载文件 File.Copy(serverPath + "/" + filename, tempFolder + "/" + filename);
ASP.NET批量下载文件 }
ASP.NET批量下载文件 //ZKHelper.JSHelper.Alert("图片拷贝成功!");
ASP.NET批量下载文件 //产生RAR文件,及文件输出
ASP.NET批量下载文件 RARSave(tempFolder, tempName);
ASP.NET批量下载文件 DownloadRAR(tempFolder + "\\\\" + tempName + ".rar");
ASP.NET批量下载文件

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

C# 代码   复制
ASP.NET批量下载文件
ASP.NET批量下载文件/// <summary>
ASP.NET批量下载文件 /// 生成RAR文件
ASP.NET批量下载文件 /// </summary>
ASP.NET批量下载文件 /// <param name="path">存放复制文件的目录</param>
ASP.NET批量下载文件 /// <param name="rarPatch">RAR文件存放目录</param>
ASP.NET批量下载文件 /// <param name="rarName">RAR文件名</param>
ASP.NET批量下载文件 private void RARSave(string rarPatch, string rarName)
ASP.NET批量下载文件 {
ASP.NET批量下载文件 string the_rar;
ASP.NET批量下载文件 RegistryKey the_Reg;
ASP.NET批量下载文件 Object the_Obj;
ASP.NET批量下载文件 string the_Info;
ASP.NET批量下载文件 ProcessStartInfo the_StartInfo;
ASP.NET批量下载文件 Process the_Process;
ASP.NET批量下载文件 try
ASP.NET批量下载文件 {
ASP.NET批量下载文件 the_Reg = Registry.ClassesRoot.OpenSubKey(@"WinRAR");
ASP.NET批量下载文件 the_Obj = the_Reg.GetValue("");
ASP.NET批量下载文件 the_rar = the_Obj.ToString();
ASP.NET批量下载文件 the_Reg.Close();
ASP.NET批量下载文件 the_rar = the_rar.Substring(1, the_rar.Length - 7);
ASP.NET批量下载文件 the_Info = " a " + rarName + " -r";
ASP.NET批量下载文件 the_StartInfo = new ProcessStartInfo();
ASP.NET批量下载文件 the_StartInfo.FileName = "WinRar";//the_rar;
ASP.NET批量下载文件 the_StartInfo.Arguments = the_Info;
ASP.NET批量下载文件 the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
ASP.NET批量下载文件 //打包文件存放目录
ASP.NET批量下载文件 the_StartInfo.WorkingDirectory = rarPatch;
ASP.NET批量下载文件 the_Process = new Process();
ASP.NET批量下载文件 the_Process.StartInfo = the_StartInfo;
ASP.NET批量下载文件 the_Process.Start();
ASP.NET批量下载文件 the_Process.WaitForExit();
ASP.NET批量下载文件 the_Process.Close();
ASP.NET批量下载文件 }
ASP.NET批量下载文件 catch (Exception)
ASP.NET批量下载文件 {
ASP.NET批量下载文件 throw;
ASP.NET批量下载文件 }
ASP.NET批量下载文件 }
ASP.NET批量下载文件

3、DownloadRAR(string file)方法

C# 代码   复制
ASP.NET批量下载文件
ASP.NET批量下载文件/// <summary>
ASP.NET批量下载文件 /// 下载生成的RAR文件
ASP.NET批量下载文件 /// </summary>
ASP.NET批量下载文件 private void DownloadRAR(string file)
ASP.NET批量下载文件 {
ASP.NET批量下载文件 FileInfo fileInfo = new FileInfo(file);
ASP.NET批量下载文件 Response.Clear();
ASP.NET批量下载文件 Response.ClearContent();
ASP.NET批量下载文件 Response.ClearHeaders();
ASP.NET批量下载文件 Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);
ASP.NET批量下载文件 Response.AddHeader("Content-Length", fileInfo.Length.ToString());
ASP.NET批量下载文件 Response.AddHeader("Content-Transfer-Encoding", "binary");
ASP.NET批量下载文件 Response.ContentType = "application/octet-stream";
ASP.NET批量下载文件 Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
ASP.NET批量下载文件 Response.WriteFile(fileInfo.FullName);
ASP.NET批量下载文件 Response.Flush();
ASP.NET批量下载文件 string tempPath = file.Substring(0, file.LastIndexOf("\\\\"));
ASP.NET批量下载文件 //删除临时目录下的所有文件
ASP.NET批量下载文件 DeleteFiles(tempPath);
ASP.NET批量下载文件 //删除空目录
ASP.NET批量下载文件 Directory.Delete(tempPath);
ASP.NET批量下载文件 Response.End();
ASP.NET批量下载文件 }
ASP.NET批量下载文件

4、DeleteFiles(string tempPath) 方法

C# 代码   复制
ASP.NET批量下载文件
ASP.NET批量下载文件/// <summary>
ASP.NET批量下载文件 /// 删除临时目录下的所有文件
ASP.NET批量下载文件 /// </summary>
ASP.NET批量下载文件 /// <param name="tempPath">临时目录路径</param>
ASP.NET批量下载文件 private void DeleteFiles(string tempPath)
ASP.NET批量下载文件 {
ASP.NET批量下载文件 DirectoryInfo directory = new DirectoryInfo(tempPath);
ASP.NET批量下载文件 try
ASP.NET批量下载文件 {
ASP.NET批量下载文件 foreach (FileInfo file in directory.GetFiles())
ASP.NET批量下载文件 {
ASP.NET批量下载文件 if (file.Attributes.ToString().IndexOf("ReadOnly") != -1)
ASP.NET批量下载文件 {
ASP.NET批量下载文件 file.Attributes = FileAttributes.Normal;
ASP.NET批量下载文件 }
ASP.NET批量下载文件 File.Delete(file.FullName);
ASP.NET批量下载文件 }
ASP.NET批量下载文件 }
ASP.NET批量下载文件 catch (Exception)
ASP.NET批量下载文件 {
ASP.NET批量下载文件 throw;
ASP.NET批量下载文件 }
ASP.NET批量下载文件 }
ASP.NET批量下载文件