Displayerror of textfile in browser Displayerror of textfile in browser google-chrome google-chrome

Displayerror of textfile in browser


Your server will most likely send the .txt file as Content-Type: text/plain, but no character set. Thus, the browser has to pick something (most likely ASCII, iso-8859-1 or iso-8859-15) and will display the UTF-8 bytes as garbage.

One workaround is to wrap your text-file in a little PHP script and send the correct encoding with it:

<?php header ('Content-Type: text/plain; charset=utf-8');readfile ('test.txt');?> 

readfile() will dump the contents of test.txt unaltered to your browser.

Note that is the webserver that picks the Content-Type based on the extension (.txt); you can probably change that, but you'd have to dig deep in the configuration files.


With UTF-8 text, browsers have a hard time figuring out the used encoding, and probably default to the system's encoding. Users would have to manually change the encoding (e.g. in Firefox, View > Character Encoding > Unicode (UTF-8) -- not a very workable solution).

One way to fix this is to configure the web server to send the text with the right Content-Type: text/plain; charset=utf-8 meta data (or via PHP, as suggested by JvO).

Or, you could try re-encoding the text file in an encoding that is easier to detect, e.g. UTF-16 with a BOM (Byte Order Mark). In Vim, save the file via:

 :setlocal bomb :w ++enc=utf16-le