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

标签 iis

我有一个临时目录:

c:\inetpub\mysite\temp\

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

一些研究,我发现您可以将 web.config 放在与文件相同的目录中以配置 HTTP header ,然后执行以下操作:
<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 已经为 .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>

关于iis - 从 web.config 设置文件的 MIME 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27747987/

相关文章:

asp.net - 更改为 Asp.net 4.0 得到 "The page cannot be found"

仅支持 TLS 1.0 或更低版本的浏览器的 IIS 8 重定向

c# - IIS应用程序池回收和 native 程序集

asp.net - 在我的IIS中部署两个网站并使用我的机器名访问

.net - HttpContext 与 HttpListenerContext

asp.net - System.Drawing:GDI+ 未正确初始化(​内部 GDI+ 错误​)

C# MySQL 中的连接太多

php - 简单的 IIS 安装 php

c# - 使用 ASP.NET MVC 发送时事通讯时出现 IIS 错误

asp.net - HTTPWebRequest "PUT"错误状态 405 IIS7 中不允许的方法