一、实现步骤

在用户操作界面,由用户选择需要下载的文件,系统根据所选文件,在服务器上创建用于存储所选文件的临时文件夹,将所选文件拷贝至临时文件夹。然后调用 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) 方法

复制代码代码如下:
/// <summary>
/// 生成RAR文件
/// </summary>
/// <param name="path">存放复制文件的目录</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)方法

复制代码代码如下:
/// <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) 方法

复制代码代码如下:
/// <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;
    }
}

DEMO下载地址:https://dwz.cn/Jw3z6fVq

ASP.NET批量下载文件的方法的更多相关文章

  1. ASP.NET批量下载文件

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

  2. 批量下载文件asp.net

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

  3. asp.net mvc5 下载文件方法

    控制器自带的 FileContentResult 可以让我们很方便的返回文件到服务端,减少了很多步骤.用于下载文件的时候,像视频.文本.图片这种浏览器支持的文件,默认就会被浏览器打开.这时候想让它变成 ...

  4. linux 从百度网盘下载文件的方法

    linux 从百度网盘下载文件的方法 发表于2015 年 月 日由shenwang 方法1.wget wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括 ...

  5. .net下载文件的方法

    最近做项目遇到文件下载的问题,原本采用的是直接用一个href链接到需要下载的文件来处理这个问题,后来发现,如果文件是一个图片,浏览器会自动打开图片而不是下载,需要用户右击另存为才可以下载,很不友好,后 ...

  6. java批量下载文件为zip包

    批量下载文件为zip包的工具类 package com.meeno.trainsys.util; import javax.servlet.http.HttpServletRequest; impor ...

  7. C#异步批量下载文件

    C#异步批量下载文件 实现原理:采用WebClient进行批量下载任务,简单的模拟迅雷下载效果! 废话不多说,先看掩饰效果: 具体实现步骤如下: 1.新建项目:WinBatchDownload 2.先 ...

  8. .net中下载文件的方法(转)

    .net中下载文件的方法 一.//TransmitFile实现下载      protected void Button1_Click(object sender, EventArgs e)      ...

  9. Javaweb向服务器上传文件以及从服务器下载文件的方法

    先导入jar包 点击下载 commons-fileupload是Apache开发的一款专门用来处理上传的工具,它的作用就是可以从request对象中解析出,用户发送的请求参数和上传文件的流. comm ...

随机推荐

  1. rsync同步工具的配置与使用

    一.什么是rsync?rsync是一款开源的,快速的,多功能的,可实现全量及增量的本地或远程数据同步备份的优秀工具. rsync官网 http://rsync.samba.org/ 二.rsync的工 ...

  2. 如何获取google Api Key

    1.使用keytool申请获取MD5值 1.1 找到你的debug.keystore Windows Vista/7: C:\Users\<username>\.android\debug ...

  3. table-layout 显示规则以及其他一些零碎的东西

    首先对中文显示的不够好 对中文失效  auto是表格的宽和高都会随着内容增多而改变  而fixed只会增加表格的高度   宽度不会发生改变  table中的td的宽,高会根据内容的多少而变化: fix ...

  4. hadoop 启动的时候datanode报错 Problem connecting to server

    刚配置好的hadoop,namenode可以正常开启,但是datanode却不停的报错,并且不能正常启动: 2014-05-04 10:43:33,970 WARNorg.apache.hadoop. ...

  5. iOS.AutoLayout.2.CustomView-with-AutoLayout

    Custom View Which Support AutoLayout 创建支持AutoLayout的Custom View AutoLayout 通过使view更加的自组织来减轻controlle ...

  6. 在iOS中将string转成UTF-8编码

    在iOS开发中,特别是在对Web服务调用的时候,经常会遇到请求参数为中文的情况,那么这时候就需要将Url转成UTF-8编码才能进行请求.使用stringByAddingPercentEscapesUs ...

  7. mysql官网

    https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/

  8. Null value was assigned to a property of primitive type setter of cn.itcast.oa.domain.Forum.topicCount

    [引用http://m.blog.csdn.net/blog/u013998070/41087351] Null value was assigned to a property of primiti ...

  9. Sliding Window Maximum LT239

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  10. Longest Turbulent Subarray LT978

    A subarray A[i], A[i+1], ..., A[j] of A is said to be turbulent if and only if: For i <= k < j ...