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

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

ASP.NET批量下載服務器端指定目錄文件

2019-11-17 01:38:09
字體:
來源:轉載
供稿:網友

asp.net批量下載服務器端指定目錄文件

 1             //遍歷服務器指定文件夾下的所有文件 2             string path = "uploads/Image/"; 3             string serverPath = Server.MapPath(path); 4  5             //創建臨時文件夾 6             string tempName = DateTime.Now.ToString("yyyyMMddHHMMss"); 7             string tempFolder = Path.Combine(serverPath, tempName); 8             Directory.CreateDirectory(tempFolder); 9 10             DirectoryInfo folder = new DirectoryInfo(serverPath);11             foreach (FileInfo file in folder.GetFiles())12             {13                 string filename = file.Name;14                 File.Copy(serverPath + "/" + filename, tempFolder + "/" + filename);15             }16             //ZKHelper.JSHelper.Alert("圖片拷貝成功!");17             //產生RAR文件,及文件輸出18             RARSave(tempFolder, tempName);19             DownloadRAR(tempFolder + "http://" + 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         { 9             string the_rar;10             RegistryKey the_Reg;11             Object the_Obj;12             string the_Info;13             ProcessStartInfo the_StartInfo;14             Process the_Process;15             try16             {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             }35             catch (Exception)36             {37                 throw;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();17             string tempPath = file.Substring(0, file.LastIndexOf("http://"));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); 8             try 9             {10                 foreach (FileInfo file in directory.GetFiles())11                 {12                     if (file.Attributes.ToString().IndexOf("ReadOnly") != -1)13                     {14                         file.Attributes = FileAttributes.Normal;15                     }16                     File.Delete(file.FullName);17                 }18             }19             catch (Exception)20             {21                 throw;22             }23         }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 六枝特区| 嘉兴市| 松阳县| 察隅县| 岗巴县| 夏河县| 樟树市| 西安市| 高碑店市| 余庆县| 巴马| 邵阳市| 昭平县| 甘德县| 巴南区| 三门县| 探索| 德庆县| 农安县| 凤山市| 江安县| 五原县| 京山县| 尼木县| 凤山市| 科技| 辽宁省| 鹰潭市| 苍山县| 襄城县| 丹凤县| 富平县| 婺源县| 松原市| 黎平县| 阿合奇县| 进贤县| 永仁县| 华阴市| 盐亭县| 永仁县|