【.net】在ASP.NET中,IE与Firefox下载文件名中带中文汉字的文件,文件名乱码的问题

#问题:客户端为ie或Firefox,服务端为asp.net时,下载文件名中包含中文汉字时,下载下来的文件的文件名是乱码;

#解决方案:

  示例代码:下载名称中带汉字的文件;

 public void ProcessRequest(HttpContext context)
2 {
string action = context.Request["action"].ToString();
if (action == "download")
{
string fileName = "资源统计详细信息.txt";//客户端保存的文件名
string filePath = context.Server.MapPath("资源统计详细信息.txt");//服务端文件路径 //以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, , bytes.Length);
fs.Close(); #region 处理中文文件名乱码问题(ie及firefox下)
Encoding encoding;
string outputFileName = null;
string browser = HttpContext.Current.Request.UserAgent.ToUpper();
if (browser.Contains("MS") == true && browser.Contains("IE") == true)
{
outputFileName = HttpUtility.UrlEncode(fileName);
encoding = System.Text.Encoding.Default;
}
else if (browser.Contains("FIREFOX") == true)
{
outputFileName = fileName;
encoding = System.Text.Encoding.GetEncoding("GB2312");
}
else
{
outputFileName = HttpUtility.UrlEncode(fileName);
encoding = System.Text.Encoding.Default;
}
#endregion HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentEncoding = encoding;
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.txt", string.IsNullOrEmpty(outputFileName) ? DateTime.Now.ToString("yyyyMMddHHmmssfff") : outputFileName));
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.End(); }
}

------------------------------

上一篇:阿里面试官问我: LRU 和 Innodb Buffer Pool 有什么关系?


下一篇:如何解决Mac里面解压后文件名乱码问题