服务器下载文件

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