从 web.config 设置文件的 MIME 类型

Setting MIME type of a file from web.config

我有一个临时目录:

c:\inetpub\mysite\temp\

我动态构建一个文本文件并将其保存到该目录。然后我让用户浏览器通过将文件放入 iframe 来下载该文件。这适用于浏览器无法打开的文件(.doc、.zip),但因为我使用的是 .txt 文件,浏览器只能打开它。例如它永远不会经历正常的下载过程,您可以在其中选择要下载它的位置。

稍微研究一下,我发现您可以将 web.config 放在与配置 HTTP headers 的文件相同的目录中,然后执行类似以下操作:

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

但是当我这样做时,我得到 Cannot add duplicate collection entry of type 'mimeMap' with unique key attribute 'fileExtension' set to '.txt' 所以我猜测 web.config 在 parent 级别已经设置了 .txt 的 MIME 类型。

是否可以使用 web.congig 文件为叶目录(或特定目录)中的静态内容设置 MIME 类型?

I found it。您必须先删除另一个才能添加。

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