隐藏

C#怎么实现文件下载功能的四种方法

发布:2020/9/15 17:55:58作者:管理员 来源:本站 浏览次数:1092

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.IO;  
  11.   
  12. public partial class _Default : System.Web.UI.Page   
  13. {  
  14.     protected void Page_Load(object sender, EventArgs e)  
  15.     {  
  16.   
  17.     }  
  18.   
  19.     //TransmitFile实现下载  
  20.     protected void Button1_Click(object sender, EventArgs e)  
  21.     {  
  22.         /* 
  23.          微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite 
  24.          下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。 
  25.          代码如下: 
  26.          */  
  27.   
  28.         Response.ContentType = "application/x-zip-compressed";  
  29.         string FileName = "test.doc";  
  30.         //使用UTF-8对文件名进行编码  
  31.         Response.AppendHeader("Content-Disposition""attachment;filename=\"" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + "\"");  
  32.         Response.ContentType = "application/octet-stream";  
  33.         Response.AddHeader("Content-Disposition""attachment;filename=" + FileName);  
  34.         string filename = Server.MapPath("../ReportTemplate/test.doc");  
  35.         Response.TransmitFile(filename);  
  36.     }  
  37.   
  38.     //WriteFile实现下载  
  39.     protected void Button2_Click(object sender, EventArgs e)  
  40.     {  
  41.          /* 
  42.          using System.IO;          
  43.          */  
  44.         string fileName = "test.doc";//客户端保存的文件名  
  45.         string filePath = Server.MapPath("../ReportTemplate/test.doc");//路径  
  46.         FileInfo fileInfo = new FileInfo(filePath);  
  47.         Response.Clear();  
  48.         Response.ClearContent();  
  49.         Response.ClearHeaders();  
  50.         Response.AddHeader("Content-Disposition""attachment;filename=\"" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + "\"");  
  51.         Response.AddHeader("Content-Length", fileInfo.Length.ToString());  
  52.         Response.AddHeader("Content-Transfer-Encoding""binary");  
  53.         Response.ContentType = "application/octet-stream";  
  54.         Response.WriteFile(fileInfo.FullName);  
  55.         Response.Flush();  
  56.         Response.End();  
  57.     }  
  58.   
  59.     //WriteFile分块下载  
  60.     protected void Button3_Click(object sender, EventArgs e)  
  61.     {  
  62.         string fileName = "test.doc";//客户端保存的文件名  
  63.         string filePath = Server.MapPath("../ReportTemplate/test.doc");//路径  
  64.   
  65.   
  66.         System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);  
  67.   
  68.   
  69.         if (fileInfo.Exists == true)  
  70.         {  
  71.             const long ChunkSize = 102400; //100K 每次读取文件,只读取100K,这样可以缓解服务器的压力  
  72.             byte[] buffer = new byte[ChunkSize];  
  73.   
  74.   
  75.             Response.Clear();  
  76.             System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);  
  77.             long dataLengthToRead = iStream.Length; //获取下载的文件总大小  
  78.             Response.ContentType = "application/octet-stream";  
  79.             Response.AddHeader("Content-Disposition",  
  80.                 "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));  
  81.             while (dataLengthToRead > 0 && Response.IsClientConnected)  
  82.             {  
  83.                 int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize)); //读取的大小  
  84.                 Response.OutputStream.Write(buffer, 0, lengthRead);  
  85.                 Response.Flush();  
  86.                 dataLengthToRead = dataLengthToRead - lengthRead;  
  87.             }  
  88.             Response.Close();  
  89.         }  
  90.     }  
  91.   
  92.     //流方式下载  
  93.     protected void Button4_Click(object sender, EventArgs e)  
  94.     {  
  95.         string fileName = "test.doc";//客户端保存的文件名  
  96.         string filePath = Server.MapPath("../ReportTemplate/test.doc");//路径  
  97.   
  98.         //以字符流的形式下载文件  
  99.         FileStream fs = new FileStream(filePath, FileMode.Open);  
  100.         byte[] bytes = new byte[(int)fs.Length];  
  101.         fs.Read(bytes, 0, bytes.Length);  
  102.         fs.Close();  
  103.         Response.ContentType = "application/octet-stream";  
  104.         //通知浏览器下载文件而不是打开  
  105.         Response.AddHeader("Content-Disposition""attachment;  filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));  
  106.         Response.BinaryWrite(bytes);  
  107.         Response.Flush();  
  108.         Response.End();  
  109.     }