Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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>
share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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