tags:

views:

78

answers:

2

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?

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.

A: 

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

Eric
So ... when renaming file, what does the .html extension add to a regulat text file to be read and treated as an html document?
volvox
It tells the web server what content-type the particular file is.
Joe
@volvox: Most web servers (including IIS) have file mappings that convert file extensions to MIME types (.txt => text/plain, .html => text/html, etc)--browsers use this type information to determine how to interpret content
rpetrich
WEll, i have not been able to fool any local browser or internal www document viewer to treat my .txt file as .html file. No web server is concerned at this level. I understand that only the .htm or .html extension is necessary.
volvox
See Laurence Gonsalves's answer.
reinierpost
+3  A: 

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

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 .txt maps 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:

In Firefox, ExternalHelperAppService is 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 .txt mapping to 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 .txt to text/html you should get the behavior you want.

Laurence Gonsalves