在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();

  

  

目录
相关文章
|
3月前
|
C# Windows
.NET开源免费的Windows快速文件搜索和应用程序启动器
今天大姚给大家分享一款.NET开源(MIT License)、免费、功能强大的Windows快速文件搜索和应用程序启动器:Flow Launcher。
|
1月前
|
存储 对象存储 Python
`openpyxl`是一个用于读写Excel 2010 xlsx/xlsm/xltx/xltm文件的Python库。它不需要Microsoft Excel,也不需要.NET或COM组件。
`openpyxl`是一个用于读写Excel 2010 xlsx/xlsm/xltx/xltm文件的Python库。它不需要Microsoft Excel,也不需要.NET或COM组件。
|
1月前
|
算法 API 数据安全/隐私保护
.NET使用原生方法实现文件压缩和解压
.NET使用原生方法实现文件压缩和解压
.NET使用原生方法实现文件压缩和解压
|
1月前
|
存储 C#
.NET使用CsvHelper快速读取和写入CSV文件
.NET使用CsvHelper快速读取和写入CSV文件
|
2月前
|
存储 安全 Unix
【.Net Core】深入理解IO之文件和目录
【.Net Core】深入理解IO之文件和目录
37 4
|
2月前
|
存储 C#
.NET使用CsvHelper快速读取和写入CSV文件
优秀项目和框架精选 该项目已收录到C#/.NET/.NET Core优秀项目和框架精选中,关注优秀项目和框架精选能让你及时了解C#、.NET和.NET Core领域的最新动态和最佳实践,提高开发工作效率和质量。坑已挖,欢迎大家踊跃提交PR推荐或自荐(让优秀的项目和框架不被埋没🤞)。
|
2月前
|
开发框架 .NET Linux
【.NET Developer】已发布好的.NET Core项目文件如何打包为Docker镜像文件
该文介绍了如何不使用VS2019手动创建ASP.NET Core Blazor项目的Dockerfile并构建Docker镜像。首先,创建名为Dockerfile的文件,并复制提供的Dockerfile内容,该文件指定了基础镜像和工作目录。然后,通过CMD在项目目录下运行`docker build -t 自定义镜像名 .`来生成镜像。最后,使用`docker run`命令启动容器并验证项目运行。此外,文章还提到了将镜像推送到Azure Container Registry (ACR)的步骤。
129 0
|
10月前
|
对象存储
.net core 阿里云接口之拷贝文件
紧接上文, 1)[.net core 阿里云接口之获取临时访问凭证](https://developer.aliyun.com/article/1363447?spm=a2c6h.12873639.article-detail.7.2b0e5b1cpeWbZ5 ".net core 阿里云接口之获取临时访问凭证") 2)[.net core 阿里云接口之将指定的OSS文件下载到流](https://developer.aliyun.com/article/1363886 ".net core 阿里云接口之将指定的OSS文件下载到流") 本文继续阿里云接口调用,将指定的OSS文件下载到流。
52 0
|
10月前
|
C#
.net core 从(本地)服务器获取APK文件并解析APK信息
## 1、apk解析除了使用客户端利用aapt.exe、unzip.exe开发客户端解析外,还可以直接利用服务进行解析 ```csharp /// <summary> /// 从本地服务器获取APK文件并解析APK信息 /// </summary> /// <param name="fileName">APK文件的完整路径</param> /// <returns></returns> [HttpPost, HttpGet, HttpOptions, CorsOptions] public IActionResult DecodeAPK(string fileName) { if(fi
67 0
|
10月前
|
JavaScript 前端开发 关系型数据库
.net core + vue + elementui 删除指定日期段、指定路径下的所有文件
# 1、呈现效果 ![image.png](https://ucc.alicdn.com/pic/developer-ecology/j2ygdazy447va_0782583bbc894c33a079db9e44385acd.png) # 2、后端 ## 1)服务层 ```csharp /// <summary> /// 删除指定修改日期段及指定路径下的所有文件 /// </summary> /// <param name="filepath">指定路径</param> /// <returns>返回删除结果提示</returns> public string DeleteSpecif
59 0