asp.net MVC 的下載方法app
引用using Ionic.Zip;asp.net
<tr> <td><a href="\Upload\Download">下載</a></td> </tr>
//下載zip文件 public ActionResult Download() { int count = 0; string ProcessInstanceID = "1"; if (!string.IsNullOrEmpty(ProcessInstanceID)) { string path = Server.MapPath("~/省略/"); //須要壓縮的照片 string zipDownLoad = Server.MapPath("~/省略/"); string filePath = zipDownLoad + "KPI_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".zip"; //保存文件路徑+文件名稱 DataTable Dt = new DataTable(); Dt.Columns.Add("FileName", System.Type.GetType("System.String")); Dt.Rows.Add(new object[] { " 圖片.PNG" }); using (ZipFile zip = new ZipFile(System.Text.Encoding.UTF8)) { if (Dt != null && Dt.Rows.Count > 0) { foreach (DataRow filedr in Dt.Rows) { if (filedr["FileName"] != DBNull.Value && filedr["FileName"] != null && !string.IsNullOrEmpty(filedr["FileName"].ToString())) { zip.AddFile(path + filedr["FileName"].ToString(), ""); //第二個參數是去除嵌套文件 } } } else { return Content("<script>alert('無數據');location.href='/SalesTake/SalesIndex'</script>"); } zip.Save(filePath); //下載 if (System.IO.File.Exists(filePath)) { FileInfo file = new FileInfo(filePath); Response.ContentEncoding = System.Text.Encoding.UTF8; //解決中文亂碼 Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解決中文文件名亂碼 Response.AddHeader("Content-length", file.Length.ToString()); Response.ContentType = "appliction/octet-stream"; Response.WriteFile(file.FullName); Response.Flush(); Response.End(); count = 1; } } } return View(); }