您的位置:首页 > 编程语言 > ASP

Asp.net 解决下载乱码问题,支持火狐、IE、谷歌等主流浏览器

2016-08-15 16:17 766 查看
public static void DownFileStream(MemoryStream ms, string fileName)
{
if (ms !=Stream.Null)
{

if (HttpContext.Current.Request.UserAgent != null && HttpContext.Current.Request.UserAgent.ToUpper().IndexOf("FIREFOX", StringComparison.Ordinal) != -1)
{
fileName = "=?UTF-8?B?" + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(fileName)) + "?=";
}
else
{
fileName = System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
if (fileName != null) fileName = fileName.Replace("+", "%20");
}
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.xls",fileName));
HttpContext.Current.Response.AddHeader("Content-Length", ms.Length.ToString());
HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
HttpContext.Current.Response.ContentType = "application/octet-stream;charset=utf-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
ms.Close();
ms.Dispose();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: