网络.配置设置相当于htaccess Header set Content-disposition

web.config setting equivalent to htaccess Header set Content-disposition

本文关键字:Header set Content-disposition htaccess 相当于 配置 设置 网络      更新时间:2023-09-26

为了下载pdf作为文件而不从服务器打开,我使用了一个很好的脚本download.js,它在Chrome中非常出色。但是正如作者在FF中警告的那样,它在一个单独的标签中打开下载的PDF -导致我的SPA导航问题。

他说在Apache这个代码在htaccess将修复它。我的应用程序在iis8上运行。我更喜欢在应用程序网络中处理它。如果可能的话配置。我可以把什么放进系统。webServer在我的Web。配置和/或管理在我的IIS共享主机提供商允许。(当然会在下面的代码中将pdf添加到FilesMatch中。)

//Easiest way to configure headers via Apache is to set Header set Content-Disposition "attachment" for files you want to be downloaded.
//So .htaccess can look like:
<FilesMatch "'.(zip|rar)$">
  Header set Content-Disposition attachment
</FilesMatch>

多亏了在serverfault上发现的这一点(之前不知道这个站点),我能够将一个与上面问题中的htaccess语言等效的出站规则放在一起。(并在此过程中学到了一些奇妙的东西)我现在得到了我一直在寻找的Firefox下载行为,尽管我仍然需要找出处理未找到资源错误的最佳方法-欢迎提出建议。

<system.webServer>
<rewrite>
  <outboundRules>
    <rule name="Allow pdfs to be downloaded" preCondition="Only match pdfs">
      <match serverVariable="RESPONSE_Content_Disposition" pattern="(.*)" negate="false" />
      <action type="Rewrite" value="attachment" replace="true" />
      <conditions>
        <add input="{QUERY_STRING}" pattern="^download" />
      </conditions>
    </rule>
    <preConditions>
      <preCondition name="Only match pdfs">
        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^application/pdf" />
      </preCondition>
    </preConditions>
  </outboundRules>
</rewrite>