asp.net文件下载[转]

1 public partial class FileDownLoad : System.Web.UI.Page   
  2     {   
  3         //提供下载的文件,不编码的话文件名会乱码   
  4         private string fileName = HttpContext.Current.Server.UrlEncode("规范.rar");   
  5         private string filePath = HttpContext.Current.Server.MapPath("规范.rar");   
  6         //使用TransmifFile下载文件   
  7         protected void btnDL1_Click(object sender, EventArgs e)   
  8         {   
  9             FileInfo info = new FileInfo(filePath);   
 10             long fileSize = info.Length;   
 11             Response.Clear();   
 12             Response.ContentType = "application/x-zip-compressed";   
 13             Response.AddHeader("Content-Disposition""attachment;filename="+ fileName);   
 14             //不指明Content-Length用Flush的话不会显示下载进度   
 15             Response.AddHeader("Content-Length", fileSize.ToString());   
 16             Response.TransmitFile(filePath, 0, fileSize);   
 17             Response.Flush();   
 18             Response.Close();   
 19         }   
 20   
 21         //使用WriteFile下载文件   
 22         protected void btnDL2_Click(object sender, EventArgs e)   
 23         {   
 24             FileInfo info = new FileInfo(filePath);   
 25             long fileSize = info.Length;   
 26             Response.Clear();   
 27             Response.ContentType = "application/octet-stream";   
 28             Response.AddHeader("Content-Disposition""attachement;filename=" + fileName);   
 29             //指定文件大小   
 30             Response.AddHeader("Content-Length", fileSize.ToString());   
 31             Response.WriteFile(filePath, 0, fileSize);   
 32             Response.Flush();   
 33             Response.Close();   
 34         }   
 35   
 36         //使用OutputStream.Write分块下载文件   
 37         protected void btnDL3_Click(object sender, EventArgs e)   
 38         {   
 39             //指定块大小   
 40             long chunkSize = 102400;   
 41             //建立一个100K的缓冲区   
 42             byte[] buffer = new byte[chunkSize];   
 43             //已读的字节数   
 44             long dataToRead = 0;   
 45             FileStream stream = null;   
 46             try    
 47             {   
 48                 //打开文件   
 49                 stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);   
 50                 dataToRead = stream.Length;   
 51   
 52                 //添加Http头   
 53                 Response.ContentType = "application/octet-stream";   
 54                 Response.AddHeader("Content-Disposition""attachement;filename=" + fileName);   
 55                 Response.AddHeader("Content-Length", dataToRead.ToString());   
 56   
 57                 while (dataToRead > 0)    
 58                 {   
 59                     if (Response.IsClientConnected)   
 60                     {   
 61                         int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));   
 62                         Response.OutputStream.Write(buffer, 0, length);   
 63                         Response.Flush();   
 64                         Response.Clear();   
 65                         dataToRead -= length;   
 66                     }   
 67                     else    
 68                     {   
 69                         //防止client失去连接   
 70                         dataToRead = -1;   
 71                     }   
 72                 }   
 73             }   
 74             catch (Exception ex)   
 75             {   
 76                 Response.Write("Error:" + ex.Message);   
 77             }   
 78             finally    
 79             {   
 80                 if (stream != null)    
 81                 {   
 82                     stream.Close();   
 83                 }   
 84                 Response.Close();   
 85             }   
 86         }   
 87   
 88         //使用BinaryWrite下载文件,大文件效率不行   
 89         protected void btnDL4_Click(object sender, EventArgs e)   
 90         {   
 91             FileStream stream = null;   
 92             try    
 93             {   
 94                 //读文件,大文件一次读入会占用大量内存   
 95                 stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);   
 96                 byte[] bytes = new byte[stream.Length];   
 97                 stream.Read(bytes, 0, bytes.Length);   
 98                 stream.Close();   
 99   
100                 //添加Http头   
101                 Response.ContentType = "application/octet-stream";   
102                 Response.AddHeader("Content-Disposition""attachement;filename=" + fileName);   
103                 Response.AddHeader("Content-Length", bytes.Length.ToString());   
104                 Response.BinaryWrite(bytes);   
105                 Response.Flush();   
106             }   
107             catch (Exception ex)   
108             {   
109                 Response.Write("Error:" + ex.Message);   
110             }   
111             finally    
112             {   
113                 if (stream != null)    
114                 {   
115                     stream.Close();   
116                 }   
117                 Response.Close();   
118             }   
119         }   
120         //使用BinaryWrite分块下载文件   
121         protected void btnDL5_Click(object sender, EventArgs e)   
122         {   
123             //指定区块和缓冲区   
124             long chunkSize = 102400;   
125             byte[] buffer = new byte[chunkSize];   
126             FileStream stream = null;   
127             long dataToRead = 0;   
128             try    
129             {   
130                 stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);   
131                 dataToRead = stream.Length;   
132                 //添加Http头   
133                 Response.ContentType = "application/octet-stream";   
134                 Response.AddHeader("Content-Disposition""attachement;filename=" + fileName);   
135                 Response.AddHeader("Content-Length", dataToRead.ToString());   
136   
137                 while (dataToRead > 0)    
138                 {   
139                     if (Response.IsClientConnected)   
140                     {   
141                         int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));   
142                         Response.BinaryWrite(buffer);   
143                         Response.Flush();   
144                         Response.Clear();   
145   
146                         dataToRead -= length;   
147                     }   
148                     else    
149                     {   
150                         dataToRead = -1;   
151                     }   
152                 }   
153   
154             }   
155             catch(Exception ex)   
156             {   
157                 Response.Write("Error:" + ex.Message);   
158             }   
159             finally  
160             {   
161                 if (stream != null)    
162                 {   
163                     stream.Close();   
164                 }   
165                 Response.Close();   
166             }   
167         }   
168     }  
169 

 

以上除了第四种不推荐以外,其他的都可以,但是个人感觉分块下载的要好一点。没有仔细测试,所以可能有问题。

注意:对于中文文件名要编码才能正确显示。对于中文文件名(UTF8编码后大于153字节的中文)即使编码了,还是有问题的,大家可以参考这个链接。 http://hi.baidu.com/river_5566/blog/item/d66804cef8afb031b700c863.html

 


转自CSDN博客:http://blog.csdn.net/zabcd117/archive/2009/04/24/4107033.aspx

你可能感兴趣的:(Stream,测试,null,asp.net,buffer,byte)