(1)方法一:
string fileName="中文.xls";
string filePath = @"/UpLoad/Reports"
FileInfo file = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(filePath)+fileName);
Response.Charset = "utf-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" +HttpUtility.UrlEncode("下载文件"+".xls",System.Text.Encoding.UTF8));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
Response.WriteFile(file.FullName);
// 停止页面的执行
Response.End();
(2)方法二:
/// <summary>
/// 下载文件
/// </summary>
/// <param name="path">要下载的文件的路径</param>
/// <param name="name">下载文件的保存名称</param>
public static void downLoad(string path,string name)
{
FileStream fileStream = new FileStream(path, FileMode.Open);
long fileSize = fileStream.Length;
byte[] fileBuffer = new byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Charset = "utf-8";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Type", "application/octet-stream");// "application/octet-stream");
System.Web.HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.BinaryWrite(fileBuffer);
HttpContext.Current.Response.End();
}