ASP.NET批量下载服务器端指定目录文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ASP.NET批量下载服务器端指定目录文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3169字,纯文字阅读大概需要5分钟。
内容图文
1 // 遍历服务器指定文件夹下的所有文件 2 string path = "uploads/Image/"; 3string serverPath = Server.MapPath(path); 4 5//创建临时文件夹 6string tempName = DateTime.Now.ToString("yyyyMMddHHMMss"); 7string tempFolder = Path.Combine(serverPath, tempName); 8 Directory.CreateDirectory(tempFolder); 910 DirectoryInfo folder = new DirectoryInfo(serverPath); 11foreach (FileInfo file in folder.GetFiles()) 12 { 13string filename = file.Name; 14 File.Copy(serverPath + "/" + filename, tempFolder + "/" + filename); 15 } 16//ZKHelper.JSHelper.Alert("图片拷贝成功!"); 17//产生RAR文件,及文件输出18 RARSave(tempFolder, tempName); 19 DownloadRAR(tempFolder + "\\" + tempName + ".rar");
1 /// <summary> 2 /// 生成RAR文件 3 /// </summary> 4 /// <param name="path"> 存放复制文件的目录 </param> 5 /// <param name="rarPatch"> RAR文件存放目录 </param> 6 /// <param name="rarName"> RAR文件名 </param> 7 private void RARSave(string rarPatch, string rarName) 8 { 9string the_rar; 10 RegistryKey the_Reg; 11 Object the_Obj; 12string the_Info; 13 ProcessStartInfo the_StartInfo; 14 Process the_Process; 15try16 { 17 the_Reg = Registry.ClassesRoot.OpenSubKey(@"WinRAR"); 18 the_Obj = the_Reg.GetValue(""); 19 the_rar = the_Obj.ToString(); 20 the_Reg.Close(); 21 the_rar = the_rar.Substring(1, the_rar.Length - 7); 22 the_Info = " a " + rarName + " -r"; 23 the_StartInfo = new ProcessStartInfo(); 24 the_StartInfo.FileName = "WinRar";//the_rar;25 the_StartInfo.Arguments = the_Info; 26 the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 27//打包文件存放目录28 the_StartInfo.WorkingDirectory = rarPatch; 29 the_Process = new Process(); 30 the_Process.StartInfo = the_StartInfo; 31 the_Process.Start(); 32 the_Process.WaitForExit(); 33 the_Process.Close(); 34 } 35catch (Exception) 36 { 37throw; 38 } 39 }
下载生成的RAR文件
1 /// <summary> 2 /// 下载生成的RAR文件 3 /// </summary> 4 private void DownloadRAR(string file) 5 { 6 FileInfo fileInfo = new FileInfo(file); 7 Response.Clear(); 8 Response.ClearContent(); 9 Response.ClearHeaders(); 10 Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name); 11 Response.AddHeader("Content-Length", fileInfo.Length.ToString()); 12 Response.AddHeader("Content-Transfer-Encoding", "binary"); 13 Response.ContentType = "application/octet-stream"; 14 Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); 15 Response.WriteFile(fileInfo.FullName); 16 Response.Flush(); 17string tempPath = file.Substring(0, file.LastIndexOf("\\")); 18//删除临时目录下的所有文件19 DeleteFiles(tempPath); 20//删除空目录21 Directory.Delete(tempPath); 22 Response.End(); 23 }
1 /// <summary> 2 /// 删除临时目录下的所有文件 3 /// </summary> 4 /// <param name="tempPath"> 临时目录路径 </param> 5 private void DeleteFiles(string tempPath) 6 { 7 DirectoryInfo directory = new DirectoryInfo(tempPath); 8try 9 { 10foreach (FileInfo file in directory.GetFiles()) 11 { 12if (file.Attributes.ToString().IndexOf("ReadOnly") != -1) 13 { 14 file.Attributes = FileAttributes.Normal; 15 } 16 File.Delete(file.FullName); 17 } 18 } 19catch (Exception) 20 { 21throw; 22 } 23 }
原文:http://www.cnblogs.com/lavalike/p/3842481.html
内容总结
以上是互联网集市为您收集整理的ASP.NET批量下载服务器端指定目录文件全部内容,希望文章能够帮你解决ASP.NET批量下载服务器端指定目录文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。