Using IIS7, is there a way to force a clients browser to download a file rather than attempt to display it in-browser? In my case I want to force browsers to download all MP4 video files.
I found the answer from this blog.
Using the IIS7 Url Rewrite module you can change the Content-Disposition
header that will suggest the clients browser should download rather than attempt to display the file.
{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>