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.


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:

  <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" />
      <add input="{QUERY_STRING}" pattern="^download" />
    <preCondition name="Only match images">
      <add input="{RESPONSE_CONTENT_TYPE}" pattern="^image/" />
  • 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 '12 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 '12 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$". – Marco Miltenburg Apr 1 '12 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.