C#ASP.NET中四种文件下载方法

  1. 无意中在论坛上看到的,保存下,以后可以备用
  2. ,特别注意文件分块下载,需要使用Response.Flush()方法,调用Flush方法将缓冲的响应发送到请求客户端
  3. //TransmitFile实现下载   
  4. protected void Button1_Click(object sender, EventArgs e)   
  5. {   
  6.     Response.ContentType = "application/x-zip-compressed";   
  7.     Response.AddHeader("Content-Disposition""attachment;filename=8月份报表.zip");   
  8.     string filename = Server.MapPath("DownLoad/8月份报表.zip");   
  9.     //指定编码 防止中文文件名乱码   
  10.     Response.HeaderEncoding = System.Text.Encoding.GetEncoding("gb2312");   
  11.     Response.TransmitFile(filename);   
  12. }   
  13. //WriteFile实现下载   
  14. protected void Button2_Click(object sender, EventArgs e)   
  15. {   
  16.     string fileName = "asd.txt";//客户端保存的文件名   
  17.     string filePath = Server.MapPath("DownLoad/aaa.txt");//路径   
  18.     FileInfo fileInfo = new FileInfo(filePath);   
  19.     Response.Clear();   
  20.     Response.ClearContent();   
  21.     Response.ClearHeaders();   
  22.     Response.AddHeader("Content-Disposition""attachment;filename=" + fileName);   
  23.     Response.AddHeader("Content-Length", fileInfo.Length.ToString());   
  24.     Response.AddHeader("Content-Transfer-Encoding""binary");   
  25.     Response.ContentType = "application/octet-stream";   
  26.     Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");   
  27.     Response.WriteFile(fileInfo.FullName);   
  28.     Response.Flush();   
  29.     Response.End();   
  30. }   
  31. //WriteFile分块下载   
  32. protected void Button3_Click(object sender, EventArgs e)   
  33. {   
  34.     string fileName = "aaa.txt";//客户端保存的文件名   
  35.     string filePath = Server.MapPath("DownLoad/aaa.txt");//路径   
  36.     System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);   
  37.     if (fileInfo.Exists == true)   
  38.     {   
  39.         const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力   
  40.         byte[] buffer = new byte[ChunkSize];   
  41.         Response.Clear();   
  42.         System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);   
  43.         long dataLengthToRead = iStream.Length;//获取下载的文件总大小   
  44.         Response.ContentType = "application/octet-stream";   
  45.         Response.AddHeader("Content-Disposition""attachment; filename=" + HttpUtility.UrlEncode(fileName));   
  46.         while (dataLengthToRead > 0 && Response.IsClientConnected)   
  47.         {   
  48.             int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小   
  49.             Response.OutputStream.Write(buffer, 0, lengthRead);   
  50.             Response.Flush();   
  51.             dataLengthToRead = dataLengthToRead - lengthRead;   
  52.         }   
  53.         Response.Close();   
  54.     }   
  55. }   
  56. //流方式下载   
  57. protected void Button4_Click(object sender, EventArgs e)   
  58. {   
  59.     string fileName = "aaa.txt";//客户端保存的文件名   
  60.     string filePath = Server.MapPath("DownLoad/aaa.txt");//路径   
  61.     //以字符流的形式下载文件   
  62.     FileStream fs = new FileStream(filePath, FileMode.Open);   
  63.     byte[] bytes = new byte[(int)fs.Length];   
  64.     fs.Read(bytes, 0, bytes.Length);   
  65.     fs.Close(); www.2cto.com  
  66.     Response.ContentType = "application/octet-stream";   
  67.     //通知浏览器下载文件而不是打开   
  68.     Response.AddHeader("Content-Disposition""attachment;  filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));   
  69.     Response.BinaryWrite(bytes);   
  70.     Response.Flush();   
  71.     Response.End();   
  72. }   

你可能感兴趣的:(C#ASP.NET中四种文件下载方法)