ASP.NET批量下载文件的方法
发布时间:2020-12-15 20:31:32 所属栏目:asp.Net 来源:网络整理
导读:本篇章节讲解ASP.NET批量下载文件的方法。供大家参考研究。具体方法如下: 一、实现步骤 在用户操作界面,由用户选择需要下载的文件,系统根据所选文件,在服务器上创建用于存储所选文件的临时文件夹,将所选文件拷贝至临时文件夹。然后调用 RAR程序
本篇章节讲解ASP.NET批量下载文件的方法。分享给大家供大家参考。具体方法如下: 一、实现步骤 在用户操作界面,由用户选择需要下载的文件,系统根据所选文件,在服务器上创建用于存储所选文件的临时文件夹,将所选文件拷贝至临时文件夹。然后调用 RAR程序,对临时文件夹进行压缩,然后输出到客户端。最后删除临时文件夹。 复制代码 代码如下: //遍历服务器指定文件夹下的所有文件 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#程序设计有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-web-api – ASP.NET Web-API帮助-API生成器?
- 动态类型dynamic转换为特定类型T的方案
- asp.net – 重音法语字符
- asp.net – 使用MvcBuildViews构建Razor = true失败
- asp.net-mvc – Visual Studio 2010未将.CSHTML(Razor View
- asp-classic – 如何清除所有cookie?
- asp.net-mvc – 获取VS2010,特别是Razor视图,将自定义“dat
- asp.net – 如何防止开放重定向攻击?
- asp.net-mvc – MVC / Unity – 如何将依赖项注入自定义Fil
- asp.net-core – 在程序集’ef’上找不到’UserSecretsIdAt
推荐文章
站长推荐
- asp.net-mvc – 查找单元测试方向,显示部分视图的
- asp.net-web-api – 如何告诉RavenDB忽略属性而不
- 深入了解 Authorize 和 AllowAnonymous
- asp.net-mvc – ASP.NET MVC / EF4 / POCO /存储
- asp.net-mvc – 我必须附加什么进程才能使用IIS
- asp.net-mvc-3 – 使用mvc3下拉列表进行jquery自
- asp.net – 如何在两个或多个aspx页面之间传递会
- asp.net-mvc – 用于加密的MVC AntiForgeryToken
- asp.net – 从MSDN获取我需要的信息的最快方法是
- 将ASP.NET网站转换为原生Android / iOS应用程序
热点阅读