rboy
rboy

Reputation: 2165

IIS 8.5 setting encoding as utf-8 for static .txt files stripping line feed

I'm trying to set the encoding for .txt static files on the IIS 8.5 webserver. From what I've researched I should add this to my web.config file:

<system.webServer>
  <staticContent>
    <remove fileExtension=".txt" />
    <mimeMap fileExtension=".txt" mimeType="text/html; charset=utf-8"/>
  </staticContent>
</system.webServer>

While it seems to work as the page open the .txt files in Chrome as UTF-8 it seems to strip all the line feeds and I get one big block of non stop text (code).

On the other hand I remove those lines Chrome opens the page with Windows-1252 encoding (which is a problem because extended characters in the text file which is also encoded in utf-8 don't show up). However if I manually change the encoding on the page through Chrome settings to utf-8 everything appears perfectly including the line feeds.

What am I doing wrong, why are the line feeds being stripped when I set the encoding in the web.config file?

Upvotes: 0

Views: 3193

Answers (1)

rboy
rboy

Reputation: 2165

Okay I figured this out (accidentally while crawling the web in a obscure MSDN page).

The issue is with the way the MIME type is defined. When using "text/html" it strips out the carriage returns from the text files (I guess it's something to do with Unix vs DOS and HTML compatibility).

Anyways to preserve the carriage feed in the Windows/DOS style text files and change the default encoding to utf-8 for static .txt files, use the following:

<system.webServer>
  <staticContent>
    <remove fileExtension=".txt" />
    <mimeMap fileExtension=".txt" mimeType="text/plain; charset=utf-8"/>
  </staticContent>
</system.webServer>

Notice that I changed the "text/html" to "text/plain" and that seemed to make all the difference.

I got a clue from this MSDN website: Adding Static Content MIME Mappings

Upvotes: 3

Related Questions