/// <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();
}
文件下载方法 asp.net
本文介绍了一种在ASP.NET中实现文件下载的方法。通过设置HTTP响应头并使用Response对象读取指定路径的文件内容,实现了将服务器上的文件提供给用户下载的功能。此方法适用于需要在Web应用中提供文件下载服务的场景。

被折叠的 条评论
为什么被折叠?



