Html 如何强制浏览器将txt文件读取为html文档?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2346515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:20:20  来源:igfitidea点击:

How to force a txt file to be read as an html document by browsers?

htmlbrowser

提问by volvox

I have .txt files which are mostly (truly) html document (they have the header, body, html tags etc.). (I'm working in Windows environment here). I would like any browser to readily read them as html document (html document with normal .html suffix). Right now i have to rename the .txt file to be able to read it in the browser (Ex: myfile.txt -> myfile.txt.htm). Any trick we can apply to fool the browser right away?

我有 .txt 文件,它们主要是(真正的)html 文档(它们有标题、正文、html 标签等)。(我在这里在 Windows 环境中工作)。我希望任何浏览器都能轻松地将它们读取为 html 文档(带有普通 .html 后缀的 html 文档)。现在我必须重命名 .txt 文件才能在浏览器中读取它(例如:myfile.txt -> myfile.txt.htm)。我们可以应用任何技巧来立即愚弄浏览器吗?

Relative question: Is there any code i could add on top of those .txt file so that only .txt files with that code will be open as html document and seen as such by browsers? (code could be anything added with hexadecimal editor ot plain ascii). Thanks.

相关问题:是否有任何代码可以添加到那些 .txt 文件之上,以便只有具有该代码的 .txt 文件才能作为 html 文档打开并被浏览器视为这样?(代码可以是任何用十六进制编辑器或纯 ascii 添加的内容)。谢谢。

采纳答案by Laurence Gonsalves

Since you're reading the file directly off of your file system (ie: using a file:URL rather than http:or something else) your browser is using the extension to determine the content-type of the file. How this mapping from extension to content type is made varies from browser to browser (and also from OS to OS to a certain extent).

由于您直接从文件系统中读取文件(即:使用file:URL 而不是http:其他内容),您的浏览器正在使用扩展名来确定文件的内容类型。这种从扩展到内容类型的映射是如何从浏览器到浏览器的(在一定程度上也从操作系统到操作系统)。

First off, I should say that I'd be a bit afraid of making this sort of change. There's probably lots of code that has a hard-coded assumption that .txtmaps to text/plain, so altering that mapping is likely to expose all sorts of nasty bugs. Caveats aside, here's what you need to do:

首先,我应该说我有点害怕做出这种改变。可能有很多代码具有.txt映射到的硬编码假设text/plain,因此更改该映射可能会暴露各种令人讨厌的错误。除了注意事项之外,您还需要执行以下操作:

In Firefox, ExternalHelperAppServiceis used to determine the type of file:URIs. Note that one of the steps is to use a hard-coded list of extension to type mappings, which most likely has .txtmapping to text/plain.

在 Firefox 中,ExternalHelperAppService用于确定file:URI的类型。请注意,其中一个步骤是使用硬编码的扩展列表来类型映射,它很可能.txt映射到text/plain.

In IE the file type mappings come from OS settings. It varies a bit depending on which version of Windows you're dealing with, but usually in the same general part of the settings where you choose which program to run for each extension you can also set a mime-type for each extension. (This is also the place Firefox looks in the "the Operating System is asked for a MIME type" step mentioned on the page I linked to above, BTW.) If you sent the MIME type for .txtto text/htmlyou should get the behavior you want.

在 IE 中,文件类型映射来自操作系统设置。它根据您处理的 Windows 版本而略有不同,但通常在您选择为每个扩展运行哪个程序的设置的相同通用部分中,您还可以为每个扩展设置 mime-type。(这也是 Firefox 在我上面链接的页面上提到的“要求操作系统提供 MIME 类型”步骤中查找的位置,顺便说一句。)如果您将 MIME 类型发送.txttext/html您,您应该会得到您想要的行为。

回答by Eric

It is the HTTP headers which tells your browser what kind of data it is transfering so you have to edit the settings of your web server

它是 HTTP 标头,它告诉您的浏览器它正在传输什么样的数据,因此您必须编辑 Web 服务器的设置

回答by Eric Hoekstra

Save the text with its htm-codes in WORDPAD as OPEN Document text. Use in the name of the file the extension .htm. This worked for me.

将文本及其 htm 代码保存在 WORDPAD 中作为 OPEN Document 文本。在文件名中使用扩展名 .htm。这对我有用。