文件下载方法 asp.net

来源:互联网 发布:python 常用库 编辑:程序博客网 时间:2024/05/16 19:27

/// <summary>
/// 文件下载
/// </summary>
/// <param name="fileName">文件路径如:"~/Affix/UpFile/20101102053350.txt"</param>
public void FileDownLoad(string fileName)
{
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
string filepath = Server.MapPath(fileName); //获取文件路径
int index = filepath.LastIndexOf('//');
string name =filepath.Substring(index+1);//获取文件名
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(name,System.Text.Encoding.UTF8));//保存的文件名
Response.WriteFile(filepath);
Response.Flush();
Response.End();
}