/// <summary>
/// 下载文件
/// </summary>
/// <param name="fileName">文件路径</param>
/// <returns></returns>
public static void FileDownLoad(System.Web.UI.Page page, string fileName)
{
try
{
fileName = HttpContext.Current.Server.MapPath(fileName);
FileInfo DownloadFile = new FileInfo(fileName); //设置要下载的文件
page.Response.Clear(); //清除缓冲区流中的所有内容输出
page.Response.ClearHeaders(); //清除缓冲区流中的所有头
page.Response.Buffer = false; //设置缓冲输出为false
//设置输出流的 HTTP MIME 类型为application/octet-stream
page.Response.ContentType = "application/octet-stream";
//将 HTTP 头添加到输出流
page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
page.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
//将指定的文件直接写入 HTTP 内容输出流
page.Response.WriteFile(DownloadFile.FullName);
page.Response.Flush(); //向客户端发送当前所有缓冲的输出
//将当前所有缓冲的输出发送到客户端
//HttpContext.Current.ApplicationInstance.CompleteRequest();
page.Response.End();
}
catch (Exception ex)
{
//错误处理
}
}