这篇文章主要介绍了ASP.NET批量下载文件的方法,实例汇总了常见的asp.net实现批量下载的方法,具有一定的实用价值,需要的朋友可以参考下
本文实例讲述了ASP.NET批量下载文件的方法。分享给大家供大家参考。具体方法如下:
一、实现步骤
在用户操作界面,由用户选择需要下载的文件,系统根据所选文件,在服务器上创建用于存储所选文件的临时文件夹,将所选文件拷贝至临时文件夹。然后调用 RAR程序,对临时文件夹进行压缩,然后输出到客户端。最后删除临时文件夹。
二、代码实现
1、ASP.NET批量下载 核心代码
//批量下载 protected void Button1_Click(object sender, EventArgs e) { //遍历服务器指定文件夹下的所有文件 string path = "Uploads/"; string serverPath = Server.MapPath(path); //临时文件夹名称 string tempName = DateTime.Now.ToString("yyyyMMddHHmmssffff"); //将两个字符串组合成一个路径 string tempFolder = Path.Combine(serverPath, tempName); //创建临时文件夹 if (!Directory.Exists(tempFolder)) //如果不存在路径 { Directory.CreateDirectory(tempFolder); //创建路径 } DirectoryInfo folder = new DirectoryInfo(serverPath); foreach (FileInfo file in folder.GetFiles()) { //文件原名(文件名后面带上传时间) string fileName = file.Name; //把文件拷贝到相应的文件夹里 File.Copy(serverPath + "/" + fileName, tempFolder + "/" + fileName); //FileInfo newFile = new FileInfo(serverPath + "/" + fileName); //newFile.CopyTo(tempFolder + "/" + fileName); //文件新名字(去掉文件名后面的上传时间) string newfileName = fileName.Substring(0, fileName.LastIndexOf("_")); //给文件新名字加后缀名 newfileName += fileName.Substring(fileName.LastIndexOf('.'), fileName.Length - fileName.LastIndexOf('.')); //拷贝完成后,还要对临时文件夹里的文件进行重命名 File.Move(tempFolder + "/" + fileName, tempFolder + "/" + newfileName); //FileInfo tempFile = new FileInfo(tempFolder + "/" + fileName); //tempFile.MoveTo(Path.Combine(tempFolder, fileName)); } //生成RAR压缩文件 RARSave(tempFolder, tempName); //下载生成的RAR文件 DownloadRAR(tempFolder + "\\" + tempName + ".rar"); } /// <summary> /// 生成RAR压缩文件 /// </summary> /// <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; } } /// <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); Response.End(); } /// <summary> /// 删除临时目录及其下的所有文件 /// </summary> /// <param name="tempPath">临时目录路径</param> private void DeleteFiles(string tempPath) { //验证文件存储的路径是否存在 if (Directory.Exists(tempPath)) //如果存在路径 { //适用于空文件夹或里面有子目录,文件的文件夹 //Directory.Delete(tempPath, true); //删除路径 DirectoryInfo di = new DirectoryInfo(tempPath); di.Delete(true); //删除空目录 //Directory.Delete(tempPath); } }