C# 文件下载四方法

 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         Response.ContentType = "application/x-zip-compressed";
23         Response.AddHeader("Content-Disposition",         "attachment;filename=z.zip");
24         string filename = Server.MapPath("DownLoad/z.zip");
25         Response.TransmitFile(filename);
26     }            
27 
28     //WriteFile实现下载
29     protected void Button2_Click(object sender, EventArgs e)
30     {
31         string fileName ="asd.txt";//客户端保存的文件名
32         string filePath=Server.MapPath("DownLoad/aaa.txt");//路径
33  
34         FileInfo fileInfo = new FileInfo(filePath);
35         Response.Clear();
36         Response.ClearContent();
37         Response.ClearHeaders();
38         Response.AddHeader("Content-Disposition",         "attachment;filename=" + fileName);
39         Response.AddHeader("Content-Length",             fileInfo.Length.ToString());
40         Response.AddHeader("Content-Transfer-Encoding", "binary");
41         Response.ContentType = "application/octet-stream";
42         Response.ContentEncoding =     System.Text.Encoding.GetEncoding("gb2312");
43         Response.WriteFile(fileInfo.FullName);
44         Response.Flush();
45         Response.End();
46     }                
47 
48   //WriteFile分块下载
49   protected void Button3_Click(object sender, EventArgs e)
50   {
51     string fileName = "aaa.txt";//客户端保存的文件名
52     string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
53  
54     System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
55  
56     if (fileInfo.Exists == true)
57     {
58       const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
59       byte[] buffer = new byte[ChunkSize];
60  
61       Response.Clear();
62       System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
63       long dataLengthToRead = iStream.Length;//获取下载的文件总大小
64       Response.ContentType = "application/octet-stream";
65       Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
66       while (dataLengthToRead > 0 && Response.IsClientConnected)
67       {
68         int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
69         Response.OutputStream.Write(buffer, 0, lengthRead);
70         Response.Flush();
71         dataLengthToRead = dataLengthToRead - lengthRead;
72       }
73       Response.Close();
74     }
75   }
76  
77   //流方式下载
78   protected void Button4_Click(object sender, EventArgs e)
79   {
80     string fileName = "aaa.txt";//客户端保存的文件名
81     string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
82  
83     //以字符流的形式下载文件
84     FileStream fs = new FileStream(filePath, FileMode.Open);
85     byte[] bytes = new byte[(int)fs.Length];
86     fs.Read(bytes, 0, bytes.Length);
87     fs.Close();
88     Response.ContentType = "application/octet-stream";
89     //通知浏览器下载文件而不是打开
90     Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
91     Response.BinaryWrite(bytes);
92     Response.Flush();
93     Response.End();
94  
95   }
96 }

 摘抄自http://www.admin10000.com/document/804.html

posted @ 2014-08-19 14:30  码先行  阅读(146)  评论(0编辑  收藏  举报