text-plain的content-type导致浏览器下载文件 [英] content-type of text-plain causes browser to download of file
问题描述
我正在编写一个Web应用程序API,当有人访问URL时,它会返回文本数据。我将内容类型设置为text / plain,但是当我使用Chrome访问它时,它会下载包含信息的文件,而不是显示它。当我使用IE访问它时,它显示正常,当我访问Firefox时,它说它正在尝试访问应用程序/八位字节流,并询问我是否要下载该文件。
I'm writing a web application API, where when someone accesses a URL, it returns text data. I set the content-type to "text/plain", but when I access it with Chrome, it downloads a file that contains the information, instead of displaying it. When I access it with IE, it shows properly, and when I access with Firefox, it says that it's attempting to access an application/octet-stream, and asking me if I want to download the file.
我使用TinyHTTPProxy记录了Web服务器返回的内容,结果如下:
I recorded what was being returned by the web server using TinyHTTPProxy, and it's as follows:
[2012-03-11 16:51:45.345] INFO {TinyHTTPProxy Thread-4} HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: close
Date: Mon, 05 Mar 2012 09:49:54 GMT
Server: localhost
[2012-03-11 16:51:45.345] INFO {TinyHTTPProxy Thread-4} 10b
P,FIO,7,31.5900,0.,,0,100,0,0,30.7600,31.9600,100,1000,,,0.,16:03:14t,,0,31.5900 ,1.2,,,15,n,,,,,03/09/2012,,31.2200,,,,-0.37,-0.37,0.274456994,,,,,0,,2846732.85 ,14,4,,3989502,BSE-CSE-NYSE-PSE-NMS,,,,,0,,,0,1155872,N,,26,26,26,16:03:14,5-7-10-11-12-13-18-25-26-28-80,0
如果我将内容类型更改为应用程序 -json,然后它显示在所有浏览器上。此外,如果我将内容类型更改为text / html,它也可以工作,即使我没有返回HTML文件。
If I change the content-type to "application-json", then it displays on all browsers. Also, if I change the content-type to "text/html", it also works, even though I'm not returning an HTML file.
是否有解释为什么text / plain表现得这样?我检查确保返回的所有数据实际上都是ASCII,因为我将内容类型设置为text / plain,所以我很困惑为什么它被解释为application / octet-stream。
Is there an explanation why text/plain is behaving this way? I checked to make sure that all of the data being returned back is actually ASCII, and since I'm setting the content-type to be text/plain, I'm confused why it's being interpreted as application/octet-stream.
推荐答案
听起来你可能遇到了 Chrome问题106150 。当内容类型为text / plain时,Chrome显然(有时)决定使用嗅探逻辑。
It sounds like you may be running into Chrome Issue 106150. Chrome apparently (sometimes) decides to use "sniffing" logic when the content-type is text/plain.
可能的解决方法:
- 如果文本是Unicode,请包含BOM。这将告诉嗅探逻辑它确实是文本。
- 从文件中删除二进制外观字节。从错误报告0x00和0x1f之间的任何值看起来是二进制除了对于ESC,CR,NP,NL,HT。
- 这听起来像是使用扩展名那显然应该是text / plain(比如.txt)可能会禁用嗅探。
- If the text is Unicode, include a BOM. This will tell the sniffing logic that it really is text.
- Remove "binary looking" bytes from the file. From the bug report "Any value between 0x00 and 0x1f looks binary except for ESC, CR, NP, NL, HT".
- It sounds like using an extension that's obviously supposed to be text/plain (like .txt) might disable the sniffing.
这篇关于text-plain的content-type导致浏览器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!