支持并尊重原创!原文地址:
1 一、//TransmitFile实现下载 2 protected void Button1_Click(object sender, EventArgs e) 3 { 4 /* 5 微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite 6 下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。 7 代码如下: 8 */ 9 Response.ContentType = "application/x-zip-compressed"; 10 Response.AddHeader("Content-Disposition", "attachment;filename=z.zip"); 11 string filename = Server.MapPath("DownLoad/z.zip"); 12 Response.TransmitFile(filename); 13 } 14 15 二、//WriteFile实现下载 16 protected void Button2_Click(object sender, EventArgs e) 17 { 18 /* 19 using System.IO; 20 21 */ 22 string fileName = "asd.txt";//客户端保存的文件名 23 string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 24 25 FileInfo fileInfo = new FileInfo(filePath); 26 Response.Clear(); 27 Response.ClearContent(); 28 Response.ClearHeaders(); 29 Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); 30 Response.AddHeader("Content-Length", fileInfo.Length.ToString()); 31 Response.AddHeader("Content-Transfer-Encoding", "binary"); 32 Response.ContentType = "application/octet-stream"; 33 Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); 34 Response.WriteFile(fileInfo.FullName); 35 Response.Flush(); 36 Response.End(); 37 } 38 39 三、 //WriteFile分块下载 40 protected void Button3_Click(object sender, EventArgs e) 41 { 42 string fileName = "aaa.txt";//客户端保存的文件名 43 string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 44 45 System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); 46 47 if (fileInfo.Exists == true) 48 { 49 const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力 50 byte[] buffer = new byte[ChunkSize]; 51 52 Response.Clear(); 53 System.IO.FileStream iStream = System.IO.File.OpenRead(filePath); 54 long dataLengthToRead = iStream.Length;//获取下载的文件总大小 55 Response.ContentType = "application/octet-stream"; 56 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName)); 57 while (dataLengthToRead > 0 && Response.IsClientConnected) 58 { 59 int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小 60 Response.OutputStream.Write(buffer, 0, lengthRead); 61 Response.Flush(); 62 dataLengthToRead = dataLengthToRead - lengthRead; 63 } 64 Response.Close(); 65 } 66 } 67 68 四、//流方式下载 69 protected void Button4_Click(object sender, EventArgs e) 70 { 71 string fileName = "aaa.txt";//客户端保存的文件名 72 string filePath = Server.MapPath("DownLoad/aaa.txt");//路径 73 74 //以字符流的形式下载文件 75 FileStream fs = new FileStream(filePath, FileMode.Open); 76 byte[] bytes = new byte[(int)fs.Length]; 77 fs.Read(bytes, 0, bytes.Length); 78 fs.Close(); 79 Response.ContentType = "application/octet-stream"; 80 //通知浏览器下载文件而不是打开 81 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); 82 Response.BinaryWrite(bytes); 83 Response.Flush(); 84 Response.End(); 85 } 86 87 //---------------------------------------------------------- 88 89 public void DownloadFile( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FileBody ) 90 { 91 92 WebForm.Response.ClearHeaders(); 93 WebForm.Response.Clear(); 94 WebForm.Response.Expires = 0; 95 WebForm.Response.Buffer = true; 96 WebForm.Response.AddHeader("Accept-Language", "zh-tw"); 97 //'文件名称 98 WebForm.Response.AddHeader("content-disposition", "attachment; filename='"+System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8)+"'"); 99 WebForm.Response.ContentType = "Application/octet-stream";100 //'文件内容101 WebForm.Response.Write(FileBody);//-----------102 WebForm.Response.End();103 }104 105 106 //上面这段代码是下载一个动态产生的文本文件,若这个文件已经存在于服务器端的实体路径,则可以通过下面的函数:107 108 public void DownloadFileByFilePath( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FilePath )109 {110 WebForm.Response.ClearHeaders();111 WebForm.Response.Clear();112 WebForm.Response.Expires = 0;113 WebForm.Response.Buffer = true;114 WebForm.Response.AddHeader("Accept-Language", "zh-tw");115 //文件名称116 WebForm.Response.AddHeader("content-disposition", "attachment; filename='" + System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8) +"'" );117 WebForm.Response.ContentType = "Application/octet-stream";118 //文件内容119 WebForm.Response.Write(System.IO.File.Rea}dAllBytes(FilePath));//---------120 WebForm.Response.End();121 122 }