content-disposition attachment filename 在Firefox和IE中得到不同的结果

本文介绍了一种在不同浏览器中实现一致文件下载体验的方法。针对Firefox和IE的不同处理方式,通过特定编码确保文件名正确显示,并避免空格引起的错误。

在Firefox中需要把filename 用双引号包起来,才能得到想要的名字,不然如果含有空格,会丢掉空格后面的部分。
而IE会把空格转为_,因此也需要HttpUtility.UrlPathEncode方法处理下名字。
如果Firefox中也用HttpUtility.UrlPathEncode处理名字,空格将被替换成"%20".


1
HttpContext.Current.Response.Buffer = true; 2 HttpContext.Current.Response.ClearContent(); 3 HttpContext.Current.Response.ClearHeaders(); 4 HttpContext.Current.Response.ContentType = "Application/pdf"; 5 string doc1 = System.IO.Path.GetFileNameWithoutExtension(doc) + "_" + intNewID.ToString() + ".pdf"; 6 7 if(HttpContext.Current.Request.Browser.Browser != "IE") 8 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" +doc1+"\""); 9 else 10 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename="+ HttpUtility.UrlPathEncode( doc1)); 11 byte[] buffer=System.IO.File.ReadAllBytes(doc); 12 HttpContext.Current.Response.AddHeader("Content-Length", buffer.Length.ToString()); 13 HttpContext.Current.Response.BinaryWrite(buffer);

 

转载于:https://www.cnblogs.com/softwaredeveloper/p/4561130.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值