What determines whether a browser will open or download a .txt file?

时光总嘲笑我的痴心妄想 提交于 2020-01-14 10:33:15

问题


Let me start by saying that I am aware that you can specify the Content-Disposition in the header and use either "attachment" or "inline" and this question is NOT about that (at least not directly).

I have a file with a .txt extension. I am noticing different browser behavior dependent on the content of that .txt file. If my file contains alphanumeric characters and I paste the location of the file into the URL bar of (say) Chrome, the file opens in the browser.

If my file contains an "SI" character the file is downloaded instead of opened in the browser.

At first I thought it might be because "SI" is a control code, but "CR" and "LF" are also control codes and the file displays in the browser when they are present. The file even opens in the Chrome when there is a "DC2" character present in the .txt file.

My question is: At the file content level, what determines whether or not a file is displayed or downloaded?

(I realize this may vary by browser, but if there is something that's at least somewhat reliable, it would be helpful to know)

Thanks in advance.

EDIT (based on answer from Sam):

When the "SI" or "DC2" character is in the first 1000 or so characters of the text file it will be downloaded, if those characters are after the first 1000 or so characters the file is displayed in the browser.


回答1:


The behavior of the browser when you send it a file is going to be based on the content-type header sent by your server. If you aren't explicitly setting it yourself, your web server may attempt to make a best guess based on the first few bytes of the file.

See http://httpd.apache.org/docs/2.2/mod/mod_mime_magic.html



来源:https://stackoverflow.com/questions/19257824/what-determines-whether-a-browser-will-open-or-download-a-txt-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!