ASP.NET批量下载文件的方法_实用技巧

本文实例讲述了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) 方法

复制代码 代码如下:

/// <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;
    }
}

希望本文所述对大家的asp.net#程序设计有所帮助。

时间: 2024-08-04 11:58:05

ASP.NET批量下载文件的方法_实用技巧的相关文章

asp.net C#实现解压缩文件的方法_实用技巧

本文实例讲述了asp.net C#实现解压缩文件的方法.一共给大家介绍了三段代码,一个是简单的解压缩单个zip文件,后一个可以解压批量的大量的但需要调用ICSharpCode.SharpZipLib.dll类了,最后一个比较实例可压缩也可以解压缩了分享给大家供大家参考.具体如下: 解压缩单个文件: 复制代码 代码如下: using System.IO; using System.IO.Compression; string sourceFile=@"D:2.zip"; string d

asp.net使用Socket.Send发送信息及Socket.SendFile传输文件的方法_实用技巧

本文实例讲述了asp.net使用Socket.Send发送信息及Socket.SendFile传输文件的方法.分享给大家供大家参考,具体如下: // Displays sending with a connected socket // using the overload that takes a buffer. public static int SendReceiveTest1(Socket server) { byte[] msg = Encoding.UTF8.GetBytes("Th

asp.net简单生成XML文件的方法_实用技巧

本文实例讲述了asp.net简单生成XML文件的方法.分享给大家供大家参考,具体如下: 方式一:直接使用DataSet SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Server=127.0.0.1;User ID=sa;Password=sa;Database=northwind;Persist Security Info=True"; conn.Open(); SqlDataAdapter da

asp.net编程实现删除文件夹及文件夹下文件的方法_实用技巧

本文实例讲述了asp.net编程实现删除文件夹及文件夹下文件的方法.分享给大家供大家参考,具体如下: //获取文件夹 string path = Server.MapPath("Image"); //获取文件夹中所有图片 if (Directory.GetFileSystemEntries(path).Length > 0) { //遍历文件夹中所有文件 foreach (string file in Directory.GetFiles(path)) { //文件己存在 if

asp.net 包含文件的方法_实用技巧

在ASP.NET包含文件的方法有: 1.<% Response.WriteFile("skin/default/footer.txt")%> 2.<% server.execute("skin/default/footer.txt")%> 3.StreamReader 对象将包含文件写到 HTTP 内容流中 //me:网上说asp.net中用include也可以的.. include和Server.Execute有什么区别? server.e

浅谈ASP.NET的include的使用方法_实用技巧

我们学过的Code分离到不同文件的方法,主要包括: 程序集.dll.<inherits src>.cs.<script src>.cs.用户控件.ascx.include.Response.WriteFile() 程序集.dll:这是最高级的方法,它引用的是一个被编译为IL的DLL(程序集)文件. <inherits src>.cs:用这种方法,可以先定义一个继承Page类的新类,再在ASPX/ASCX文件中加工此类. <script src>.cs:可以把

asp.net中调用存储过程的方法_实用技巧

本文实例讲述了asp.net中调用存储过程的方法.分享给大家供大家参考,具体如下: 一.建立并调用一个不带参数的存储过程如下: CREATE PROCEDURE 全部学生<dbo.selectUsers> AS SELECT * FROM 学生 GO EXEC 全部学生 建立并调用一个带参数的存储过程如下: CREATE PROCEDURE 学生查询1 @SNAME VARCHAR(8),@SDEPT VARCHAR(20) AS SELECT * FROM 学生 WHERE 姓名=@SNAM

ASP.NET显示渐变图片实现方法_实用技巧

先给大家来个最终效果: 实现效果,首先准备一张图片,高度为25pixel,宽度为1至3pixel渐变的图片.可以这里下载. 还要准备数据: Dictionary<int, int> Datas { get { Dictionary<int, int> d = new Dictionary<int, int>(); d.Add(1, 35); d.Add(2, 45); d.Add(3, 20); return d; } } ok,数据准备完了,在aspx里放三个Labe

ASP.NET使用TreeView显示文件的方法_实用技巧

本文实例讲述了ASP.NET使用TreeView显示文件的方法,是非常实用的技巧.分享给大家供大家参考.具体实现方法如下: 通常在ASP.NET中,TreeView的使用很普遍,这里详细讲述一下使用TreeView显示文件的方法. 1.首先加入TreeView控件: <asp:TreeView ID="driverInfoView" runat="server" ImageSet="XPFileExplorer" OnTreeNodePop