在ASP.NET中,IE与Firefox下载文件带汉字名时乱码的解决方法

简介: 解决办法: HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.
+关注继续查看

解决办法:

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.Charset = "gb2312";
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", string.IsNullOrEmpty(fileName) ? DateTime.Now.ToString("yyyyMMddHHmmssfff") : System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)));
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
HttpContext.Current.Response.End();

不过上述方法在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;
                }
                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}.xls", string.IsNullOrEmpty(outputFileName) ? DateTime.Now.ToString("yyyyMMddHHmmssfff") : outputFileName));
                HttpContext.Current.Response.BinaryWrite(ms.ToArray());
                HttpContext.Current.Response.End();

  

  

目录
相关文章
|
开发框架 安全 前端开发
关于ASP.NET MVC 项目在本地vs运行响应时间过长无法访问时,解决方法!
关于ASP.NET MVC 项目在本地vs运行响应时间过长无法访问时,解决方法!
141 0
关于ASP.NET MVC 项目在本地vs运行响应时间过长无法访问时,解决方法!
|
.NET 开发框架
asp.net下ueditor上传大容量视频报http请求错误的解决方法
故障现象: 当使用百度编辑器ueditor上传大容量视频或大容量图片的时候,编辑器报“http请求错误”的解决方法详解; 原因分析: 目前很多CMS整合了百度的ueditor编辑器,但是上传稍微大一点的文件就会报错, 解决方案 1:修改相对应的ueditor\asp\config.
1747 0
|
.NET Windows 开发框架
ASP.NET Web Service中使用Session 及 Session丢失解决方法 续
原文:ASP.NET Web Service中使用Session 及 Session丢失解决方法 续 1、关于Session丢失问题的说明汇总,参考这里 2、在Web Servcie中使用Session,需要对Web Method做如下处理 [WebMethod(EnableSession = true)]public void usingSession(){    Session["Name"] = "Name";}   如果不加EnableSession = true,在Web Service中是不能使用Session的。
717 0
相关产品
云迁移中心
推荐文章
更多