天天看點

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

在Firefox中需要把filename 用雙引号包起來,才能得到想要的名字,不然如果含有空格,會丢掉空格後面的部分。

而IE會把空格轉為_,是以也需要HttpUtility.UrlPathEncode方法處理下名字。

如果Firefox中也用HttpUtility.UrlPathEncode處理名字,空格将被替換成"%20".

<a></a>

 1

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

            HttpContext.Current.Response.Buffer = true;

 2

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

            HttpContext.Current.Response.ClearContent();

 3

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

            HttpContext.Current.Response.ClearHeaders();

 4

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

            HttpContext.Current.Response.ContentType = "Application/pdf";

 5

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

            string doc1 = System.IO.Path.GetFileNameWithoutExtension(doc) + "_" + intNewID.ToString() + ".pdf";

 6

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

 7

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

            if(HttpContext.Current.Request.Browser.Browser != "IE")

 8

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

                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" +doc1+"\"");

 9

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

            else

10

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

            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename="+ HttpUtility.UrlPathEncode( doc1));

11

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

            byte[] buffer=System.IO.File.ReadAllBytes(doc);

12

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

            HttpContext.Current.Response.AddHeader("Content-Length", buffer.Length.ToString());

13

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

            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 &amp; 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.

繼續閱讀