/// <summary> /// Zip files and Download /// </summary> /// <param name="response">Current HttpResponse</param> /// <param name="zipFileToCreate">Zip save path</param> /// <param name="files">zipfile</param> public static void ResponseZip(System.Web.HttpResponse response, string zipFileToCreate, List <string> files) { try { using (ZipFile zip = new ZipFile()) { foreach (string filename in files) { ZipEntry e = zip.AddFile(filename, string.Empty); e.Comment = "Added by VAU"; } zip.Comment = string.Format( "This zip archive was created by the CreateZip example application on machine '{0}'", System.Net.Dns.GetHostName()); zip.Save(zipFileToCreate); } response.Clear(); response.AppendHeader("Content-Disposition", "attachment; filename=VAUFiles.zip"); response.ContentType = "application/x-zip-compressed"; response.WriteFile(zipFileToCreate); if (response.IsClientConnected) { response.Flush(); response.Close(); } } catch (System.Exception ex) { throw ex; } }
//download file public void saveFile(System.Web.HttpResponse Response, string PathfileName) { string filename = PathfileName; if (filename != "") { string path = filename; System.IO.FileInfo file = new System.IO.FileInfo(path); if (file.Exists) { Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); Response.AddHeader("Content-Length", file.Length.ToString()); Response.ContentType = "application/octet-stream"; Response.WriteFile(file.FullName); Response.End(); } } }
public JObject DownloadFile(string fileName) { JObject obj = new JObject(); try { System.Web.HttpRequest httpRequest = System.Web.HttpContext.Current.Request; string filePath = System.Web.HttpContext.Current.Server.MapPath("~/ApiUploadFile/"); filePath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/ApiUploadFile/"), fileName); if (File.Exists(filePath)) { System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; response.Clear(); response.ClearHeaders(); response.ClearContent(); response.Buffer = true; response.AddHeader("content-disposition", string.Format("attachment; FileName={0}", fileName)); response.Charset = "utf-8"; response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); response.ContentType = System.Web.MimeMapping.GetMimeMapping(fileName); response.WriteFile(filePath); response.Flush(); response.Close(); obj.Add("success", true); obj.Add("message", ""); } else { obj.Add("success", false); obj.Add("message", "文件不存在!"); } } catch (Exception ex) { obj.Add("success", false); obj.Add("message", ex.Message); } return(obj); }