是什么决定浏览器将打开还是下载.txt文件? [英] What determines whether a browser will open or download a .txt file?

查看:201
本文介绍了是什么决定浏览器将打开还是下载.txt文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我开始说,我知道您可以在标题中指定Content-Disposition并使用"attachment"或"inline",并且对此问题不是(至少不直接).

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).

我有一个扩展名为.txt的文件.我注意到取决于该.txt文件内容的不同浏览器行为.如果我的文件包含字母数字字符,并且我将文件的位置粘贴到(例如)Chrome的URL栏中,则该文件会在浏览器中打开.

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.

如果我的文件包含"SI" 字符,则下载文件而不是在浏览器.

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

起初我以为可能是因为"SI"是控制代码,但是"CR"和"LF"也是控制代码,如果存在,文件将显示在浏览器中.当.txt文件中存在"DC2"字符时,该文件甚至会在Chrome中打开.

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)

谢谢.

编辑(基于Sam的回答):

EDIT (based on answer from Sam):

当文本文件的前1000个左右字符中包含"SI"或"DC2"字符时,将下载该文件,如果这些字符位于前1000个左右字符后,则在浏览器中显示该文件./p>

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.

推荐答案

向浏览器发送文件时,浏览器的行为将基于服务器发送的内容类型标头.如果您没有自己明确设置,则Web服务器可能会尝试根据文件的前几个字节做出最佳猜测.

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.

请参见 http://httpd.apache.org/docs/2.2/mod/mod_mime_magic.html

这篇关于是什么决定浏览器将打开还是下载.txt文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆