国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP.NET打包下載文件

2019-11-14 15:46:48
字體:
來源:轉載
供稿:網友

使用的類庫為:ICSharpCode.SharpZipLib.dll

 

一種是打包整個文件夾,另一種是打包指定的多個文件,大同小異:

 

  1 using ICSharpCode.SharpZipLib.Zip;  2   3 public partial class _Default : System.Web.UI.Page  4 {  5     PRotected void Page_Load(object sender, EventArgs e)  6     {  7         // 打包下載某文件夾里的所有文件  8   9         //需要打包的文件夾 10         string path = "UploadImage/Images/"; 11         string serverPath = Server.MapPath(path); 12  13         //創建臨時文件夾 14         string tempName = DateTime.Now.ToString("yyyyMMddHHMMss"); 15         string tempFolder = Path.Combine(serverPath, tempName); 16         Directory.CreateDirectory(tempFolder); 17  18         //遍歷文件夾中所有的文件到臨時文件夾中 19         DirectoryInfo folder = new DirectoryInfo(serverPath); 20         foreach (FileInfo file in folder.GetFiles()) 21         { 22             string filename = file.Name; 23             File.Copy(serverPath + "/" + filename, tempFolder + "/" + filename); 24         } 25  26         //壓縮文件 27         compressFiles(tempFolder, tempFolder + "////" + tempName + ".rar"); 28         //下載文件 29         // DownloadRAR(tempFolder + "http:////" + tempName + ".rar", "這里下載文件重命名后的名稱"); 30  31  32         //某些文件打包下載 33         DownLodeSum(); 34  35     } 36  37     /// <summary> 38     /// 某些文件打包下載 39     /// </summary> 40     public void DownLodeSum() 41     { 42         //臨時文件夾所在目錄 43         string path = "UploadImage/"; 44         string serverPath = Server.MapPath(path); 45  46         //創建臨時文件夾 47         string tempName = DateTime.Now.ToString("yyyyMMddHHMMss"); 48         string tempFolder = Path.Combine(serverPath, tempName); 49         Directory.CreateDirectory(tempFolder); 50  51         //復制需要壓縮的文件到臨時文件夾中 52         File.Copy(Server.MapPath("UploadImage/Images/bg-b.png"), Server.MapPath(path + tempName + "/bg-b.png")); 53         File.Copy(Server.MapPath("UploadImage/Images/bg-j.png"), Server.MapPath(path + tempName + "/bg-j.png")); 54         File.Copy(Server.MapPath("UploadImage/Images/icon-cart.png"), Server.MapPath(path + tempName + "/icon-cart.png")); 55  56         //壓縮文件 57         compressFiles(tempFolder, tempFolder + "////" + tempName + ".rar"); 58         //下載文件 59         DownloadRAR(tempFolder + "////" + tempName + ".rar", "這里下載文件重命名后的名稱"); 60  61     } 62  63     /// <summary> 64     /// 壓縮文件 65     /// </summary> 66     /// <param name="dir">文件目錄</param> 67     /// <param name="zipfilename">zip文件名</param> 68     public static void compressFiles(string dir, string zipfilename) 69     { 70         if (!Directory.Exists(dir)) 71         { 72             return; 73         } 74         try 75         { 76             string[] filenames = Directory.GetFiles(dir); 77             using (ZipOutputStream s = new ZipOutputStream(File.Create(zipfilename))) 78             { 79  80                 s.SetLevel(9); // 0 - store only to 9 - means best compression 81  82                 byte[] buffer = new byte[4096]; 83  84                 foreach (string file in filenames) 85                 { 86                     ZipEntry entry = new ZipEntry(Path.GetFileName(file)); 87                     entry.DateTime = DateTime.Now; 88                     s.PutNextEntry(entry); 89                     using (FileStream fs = File.OpenRead(file)) 90                     { 91                         int sourceBytes; 92                         do 93                         { 94                             sourceBytes = fs.Read(buffer, 0, buffer.Length); 95                             s.Write(buffer, 0, sourceBytes); 96                         } while (sourceBytes > 0); 97                     } 98                 } 99                 s.Finish();100                 s.Close();101             }102         }103         catch104         {105 106         }107     }108 109     /// <summary>110     /// 下載生成的RAR文件111     /// </summary>112     private void DownloadRAR(string file, string name)113     {114         FileInfo fileInfo = new FileInfo(file);115         Response.Clear();116         Response.ClearContent();117         Response.ClearHeaders();118         Response.AddHeader("Content-Disposition", "attachment;filename=" + name + ".rar");119         Response.AddHeader("Content-Length", fileInfo.Length.ToString());120         Response.AddHeader("Content-Transfer-Encoding", "binary");121         Response.ContentType = "application/octet-stream";122         Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");123         Response.WriteFile(fileInfo.FullName);124         Response.Flush();125         string tempPath = file.Substring(0, file.LastIndexOf("////"));126         //刪除臨時目錄下的所有文件127         DeleteFiles(tempPath);128         //刪除空目錄129         Directory.Delete(tempPath);130         Response.End();131     }132 133     /// <summary>134     /// 刪除臨時目錄下的所有文件135     /// </summary>136     /// <param name="tempPath">臨時目錄路徑</param>137     private void DeleteFiles(string tempPath)138     {139         DirectoryInfo directory = new DirectoryInfo(tempPath);140         try141         {142             foreach (FileInfo file in directory.GetFiles())143             {144                 if (file.Attributes.ToString().IndexOf("ReadOnly") != -1)145                 {146                     file.Attributes = FileAttributes.Normal;147                 }148                 File.Delete(file.FullName);149             }150         }151         catch (Exception)152         {153             throw;154         }155     }156 }

 點擊下載源碼


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 田东县| 宣城市| 临西县| 郯城县| 台湾省| 河西区| 峡江县| 凌云县| 元氏县| 克东县| 平安县| 柳林县| 洛宁县| 莱芜市| 鄂州市| 淮滨县| 天水市| 弥勒县| 聂拉木县| 淮滨县| 游戏| 勐海县| 寿阳县| 塘沽区| 海林市| 新津县| 开远市| 扶沟县| 威远县| 酒泉市| 伊宁市| 怀宁县| 东海县| 本溪市| 红桥区| 清新县| 延津县| 扎赉特旗| 渭南市| 霍山县| 广昌县|