随笔 - 197,  文章 - 2,  评论 - 1484,  阅读 - 73万
在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);
复制代码

Firefox does not handle filenames with spaces . When a user clicks on an attachment with spaces, the filename is truncated to the first whitespace. While IE & Safari both handle this, Firefox refuses to accept mime headers with unquoted filename parameters. According to Firefox's bugzilla/knowledgebase, Firefox's behavior is the correct behavior and it's a problem with most webservers or web applications. This problem can be easily corrected by surrounding the filename parameter with double quotes.

posted on   RubyPDF  阅读(4303)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
< 2009年9月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 1 2 3
4 5 6 7 8 9 10

点击右上角即可分享
微信分享提示