解决IE11下,下载文件名乱码

本文讲述了在ASP.NETCore中,如何根据用户代理头判断浏览器类型,尤其是处理IE浏览器(包括Trident渲染引擎),并正确设置Content-Disposition头以实现文件的下载功能。
摘要由CSDN通过智能技术生成
 string path = context.Request["path"];
                        string FileName = path.Substring(path.LastIndexOf("/") + 1);
                        //设置http输出流的类型
                        context.Response.ContentType = "application/octet-stream";
                        string userAgent = context.Request.UserAgent;
                        if (userAgent != null)
                        {
                            // 检查User-Agent字符串是否包含"MSIE"或"Trident"(某些版本的IE使用Trident作为渲染引擎)  
                            if (userAgent.Contains("MSIE") || userAgent.Contains("Trident"))
                            {
                                //IE11的时候直接转
                                 FileName = HttpUtility.UrlEncode(FileName, Encoding.UTF8);
                                context.Response.AddHeader("Content-Disposition", $"attachment; filename*=UTF-8''{FileName}");
                            }
                            else
                            {
                                context.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
                            }
                        }
                        //拼接虚拟路径,获取相对应的物理路径
                        string filename = context.Server.MapPath(path);
                     
                        //http响应输出流,下载文件
                        context.Response.TransmitFile(filename);

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值