使用IIS7,是否有办法强制客户端浏览器下载文件而不是尝试在浏览器中显示?在我的情况下,我想强制浏览器下载所有MP4视频文件。
答案 0 :(得分:5)
我从this博客找到答案。
使用IIS7 Url Rewrite模块,您可以更改Content-Disposition
标题,建议客户端浏览器下载而不是尝试显示该文件。
{REQUEST_FILENAME}
\.mp4$
{REQUEST_FILENAME}
(.*)\\([^/]+)\.mp4$
Value: attachment; filename={C:2}.mp4
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
...
<rewrite>
<outboundRules>
<rule name="Forcing Download for MP4s" preCondition="FileIsMP4">
<match serverVariable="RESPONSE_Content-Disposition" pattern=".*" />
<conditions>
<add input="{REQUEST_FILENAME}" pattern="(.*)\\([^/]+)\.mp4$" />
</conditions>
<action type="Rewrite" value="attachment; filename={C:2}.mp4" />
</rule>
<preConditions>
<preCondition name="FileIsMP4">
<add input="{REQUEST_FILENAME}" pattern="\.mp4$" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
...
</system.webServer>
</configuration>
答案 1 :(得分:-1)
谢谢,这正是我所需要的。我稍微调整了一下,添加了通过附加查询字符串“?download = yes”来指定是否下载文件的功能。
<outboundRules>
<rule name="Force download with query string" preCondition="DownloadableFileTypes">
<match serverVariable="RESPONSE_Content-Disposition" pattern=".*" />
<conditions>
<add input="{REQUEST_FILENAME}" pattern="(.*)\\([^/]+)\.(.+)$" />
<add input="{QUERY_STRING}" pattern="^.*download=yes.*$" />
</conditions>
<action type="Rewrite" value="attachment; filename={C:2}.{C:3}" />
</rule>
<preConditions>
<preCondition name="DownloadableFileTypes" logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" pattern="\.mp3$" />
<add input="{REQUEST_FILENAME}" pattern="\.pdf$" />
<add input="{REQUEST_FILENAME}" pattern="\.doc$" />
<add input="{REQUEST_FILENAME}" pattern="\.docx$" />
<add input="{REQUEST_FILENAME}" pattern="\.xls$" />
<add input="{REQUEST_FILENAME}" pattern="\.xlsx$" />
<add input="{REQUEST_FILENAME}" pattern="\.mp4$" />
</preCondition>
</preConditions>
</outboundRules>