.net 下载文件
1、多文件下载并压缩
using ICSharpCode.SharpZipLib.Zip;
1 public class DownLoadZip 2 { 3 public static void Download(IEnumerable<string> files, string zipFileName, HttpResponse response) 4 { 5 6 try 7 { 8 MemoryStream ms = new MemoryStream(); 9 byte[] buffer = null; 10 using (ZipFile file = ZipFile.Create(ms)) 11 { 12 file.BeginUpdate(); 13 file.NameTransform = new MyNameTransfom(); 14 foreach (var item in files) 15 { 16 file.Add(System.Web.HttpContext.Current.Server.MapPath(item)); 17 } 18 file.CommitUpdate(); 19 buffer = new byte[ms.Length]; 20 ms.Position = 0; 21 ms.Read(buffer, 0, buffer.Length); 22 } 23 response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(zipFileName + ".zip", System.Text.Encoding.UTF8)); 24 response.BinaryWrite(buffer); 25 response.Flush(); 26 response.End(); 27 } 28 catch (Exception ex) 29 { 30 throw ex; 31 } 32 } 33 } 34 public class MyNameTransfom : ICSharpCode.SharpZipLib.Core.INameTransform 35 { 36 #region INameTransform 成员 37 public string TransformDirectory(string name) 38 { 39 return null; 40 } 41 public string TransformFile(string name) 42 { 43 return Path.GetFileName(name); 44 } 45 #endregion 46 }
2、直接下载某个文件
public static void Download1(string filePath, string fileName, HttpResponse response) { try { FileStream fs = new FileStream(System.Web.HttpContext.Current.Server.MapPath(filePath), FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); response.BinaryWrite(bytes); response.Flush(); //response.End();由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值。出错 HttpContext.Current.ApplicationInstance.CompleteRequest(); } catch (Exception ex) { throw ex; } }
1 string path = context.Request["path"]; 2 string fileName = context.Request["fileName"]; 3 //DownloadFile(Server.MapPath("/ShareAttachment_Attachment/" + path), name, id); 4 string tempPath = AppDomain.CurrentDomain.BaseDirectory; 5 try 6 { 7 8 9 tempPath = context.Request.FilePath + "00000000000" + Directory.GetParent(context.Request.FilePath); 10 System.IO.FileInfo file = new System.IO.FileInfo(Directory.GetParent(context.Request.FilePath)+"\\Announcement_PDF\\"+path); 11 context.Response.Clear(); 12 context.Response.Charset = "GB2312"; 13 context.Response.ContentEncoding = System.Text.Encoding.UTF8; 14 // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 15 context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); 16 // 添加头信息,指定文件大小,让浏览器能够显示下载进度 17 context.Response.AddHeader("Content-Length", file.Length.ToString()); 18 // 指定返回的是一个不能被客户端读取的流,必须被下载 19 context.Response.ContentType = "application/ms-excel"; 20 // 把文件流发送到客户端 21 context.Response.WriteFile(file.FullName); 22 // 停止页面的执行 23 //Response.End(); 24 HttpContext.Current.ApplicationInstance.CompleteRequest(); 25 } 26 catch (Exception ex) 27 { 28 context.Response.Write("<script>alert('系统出现以下错误://n" + tempPath + ex.Message + "!//n请尽快与管理员联系.')</script>"); 29 }