有条件地设置Content-Disposition标题

我有一个IIS7服务器上的图片库。 我想有一个button来强制下载当前可见的图像。

也许像设置一个URL参数: http : //website.com/images/img.jpg?download

全局检测此参数,然后将Content-Disposition标题设置为附件。

我的问题是,我将如何做到这一点? 我可以以某种方式将其设置在web.config文件中吗?

我更像是一个前端家伙。

是的你可以,但只有当你的服务器pipe理员允许你改变Content-Disposition头。 这可以允许在服务器或网站级别,但必须由pipe理员允许,因为它是通过applicationHost.confg文件configuration的。

这是web.config的重写规则:

<outboundRules> <rule name="Allow images to be downloaded" preCondition="Only match images"> <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 images"> <add input="{RESPONSE_CONTENT_TYPE}" pattern="^image/" /> </preCondition> </preConditions> </outboundRules>