2

I have an image gallery on a IIS7 server. I would like to have a button to force download the currently visible image.

Maybe something like setting an url parameter: http://website.com/images/img.jpg?download

Globally detect this parameter and then set the Content-Disposition header to attachment.

My question is, how would I do that? Can I set it in the web.config file somehow?

I'm more of a frontend guy.

1 Answer 1

5

Yes you can, but only if your server admin allows you to change the Content-Disposition header. This can be allowed either on the server or website level but has to be allowed by the Administrator as it's configured via the applicationHost.confg file.

Here's the rewrite rule for the 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>
3
  • Marco.. How will this work on other files? I'm trying to force download to a VCF (vCard) file. Can this code be used to force download all file types? best regards Rasmus
    – Thylle
    Mar 19, 2012 at 12:52
  • @Rasmus This code can be used for any kind of file, just make sure to point the preCondition to the rule that matches images or vcards.
    – Thor Erik
    Mar 19, 2012 at 13:12
  • As Thor said, it works for any file as long as the precondition matches the mime type of the file you want it to match. The mime type for varc is text/x-vcard, so your pattern should be pattern="^text/x-vcard$". Apr 1, 2012 at 20:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .