Asp.net直接保存文件到客户端
1
/// <summary>
2
/// 下载文件
3
/// </summary>
4
/// <param name="filename">文件物理地址</param>
5
protected void DownloadFile(string filename)
6
{
7
string saveFileName = "test.xls";
8
int intStart = filename.LastIndexOf("\\")+1;
9
saveFileName = filename.Substring(intStart,filename.Length-intStart);
10![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
11
Response.Clear();
12
Response.Charset = "utf-8";
13
Response.Buffer= true;
14
this.EnableViewState = false;
15
Response.ContentEncoding = System.Text.Encoding.UTF8;
16![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
17
Response.AppendHeader("Content-Disposition","attachment;filename=" + saveFileName);
18
Response.WriteFile(filename);
19
Response.Flush();
20
Response.Close();
21![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
22
Response.End();
23
}
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
3
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
4
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)
5
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/None.gif)
6
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
8
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
9
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
10
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
11
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
12
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
13
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
14
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
15
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
16
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
17
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
18
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
19
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
20
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
21
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
22
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/InBlock.gif)
23
![](http://walkingboy.vcblog.net/Images/OutliningIndicators/ExpandedBlockEnd.gif)