user2102611
user2102611

Reputation:

Setting MIME type of a file from web.config

I have a temporary directory at:

c:\inetpub\mysite\temp\

I build a text file dynamically and save it to that directory. Then I have the users browser download that file by putting it into an iframe. This works fine for files that the browser can't open (.doc, .zip) but because I'm using a .txt file the browser just opens it. e.g. It never goes through the normal download process where you can pick where you want to have it downloaded.

A little research and I found you can put a web.config in the same directory as the file to configure the HTTP headers and then do something like:

<configuration>
  <system.webServer>
    <staticContent>
        <mimeMap fileExtension=".txt" mimeType="application/octet-stream" />
    </staticContent>
  </system.webServer>
</configuration>

But when I do that I get Cannot add duplicate collection entry of type 'mimeMap' with unique key attribute 'fileExtension' set to '.txt' So I'm guessing at a web.config at a parent level is already setting the MIME type for .txt.

Is there a way to set the MIME type for static content in a leaf directory (or for a specific directory) using a web.congig file?

Upvotes: 4

Views: 7482

Answers (1)

user2102611
user2102611

Reputation:

I found it. You have to remove the other one before adding.

<configuration>
 <system.webServer>
        <staticContent>
            <remove fileExtension=".txt" />
            <mimeMap fileExtension=".txt" mimeType="application/octet-stream" />
        </staticContent>
 </system.webServer>
</configuration>

Upvotes: 6

Related Questions