10

I need to be able to have a subdirectory of images all PNG's to be downloaded instead of opened in the browser window. I am using IIS for the web server.

Is there a way to force a PNG to be downloadable?

RedWolves
  • 10,379
  • 12
  • 49
  • 68

5 Answers5

29

As the other posters have said, you need to add the HTTP Content-Disposition header, with a value of attachment, to the HTTP response generated by IIS when serving the PNGs in question.

Not sure what version of IIS you using are but:

IIS6

  1. In the IIS Manager select the directory with the PNG files and open the Properties dialog
  2. Click the HTTP Headers tab.
  3. In the Custom HTTP Headers section, click Add.
  4. A dialog appears. In the "Custom-header name" field enter "Content-disposition". In the "Custom-header value field, enter "Attachment".
  5. You may have to restart IIS (iisreset)

For IIS7:

  1. In the IIS Manager select the directory with the PNG files and select the Features view.
  2. Double-click the HTTP Response Headers item and then choose the Add option in the right-hand menu.
  3. A dialog appears. In the "Name" field enter "Content-disposition". In the "Value" field, enter "Attachment".
  4. You may have to restart IIS (iisreset)

Alternatively, place this web.config in the folder with files:

<?xml version="1.0"?>
<configuration>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <clear />
        <add name="Content-Disposition" value="Attachment" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

This should force the PNGs served from that directory to be downloadable.

Pavel Chuchuva
  • 22,633
  • 10
  • 99
  • 115
Amal Sirisena
  • 1,479
  • 10
  • 10
  • 1
    Hmm, I have had problems with this header in IE in the past if the filenames contained URI reserved characters eg ':'. Glad you found a workaround anyway. – Amal Sirisena Jun 25 '09 at 15:12
  • No need to reset IIS. All web.config changes work on the fly. This also works fine with IE9 and IE10. In fact, all browsers I tried (IE, Chrome, FF and Safari) this works fine for JPG, PNG, MP3 and MPG. – Justin Emlay Dec 31 '12 at 20:16
  • Worked great in the IIS Web.config to protect the contents of a particular folder that users have access for uploading files into (prevents executing any uploaded scripts and forces a download dialog). Thanks! – Andrew Bucklin Mar 31 '14 at 20:30
2

You can't if you directly serve the PNGs, but if you use ASP, you can add:

Response.AddHeader("Content-Disposition", "attachment");
MiffTheFox
  • 21,302
  • 14
  • 69
  • 94
1

You have to send this header to the browser:

Content-Disposition:attachment; filename="downloaded.pdf"

I have no clue on how to do this using IIS.

Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
1

All solutions I have tried make it so that it'll download in other browsers except IE. IE is trying to be "helpful" and decides what it thinks would best server the client, in this case is display the png file in the browser.

There is always the programatic way of doing this as been pointed out. But I wasn't looking to go that route.

In the end I individually zipped up the 67 PNG files and linked to those. It's not pretty but it works.

Thanks all for the help.

RedWolves
  • 10,379
  • 12
  • 49
  • 68
  • Thanks. Worked perfectly using server 2008 and IIS 7.5 tested in IE 9, 8 and Firefox etc... –  Oct 11 '11 at 13:37
0

Probably, my answer is outdated but hopefully it can be useful for someone...

As the other posters have said, you need to add the HTTP 'Content-Disposition' response header.

The problem could appear if you can't use IIS Manager and have to use command line.

So, if you need to add this pretty HTTP response header using command line only, you can use such construction:

%systemroot%\system32\inetsrv\APPCMD set config "Default Web Site/path/to/images" /section:httpProtocol /+customHeaders.[name='Content-disposition',value='Attachment']

It will add HTTP 'Content-Disposition' response header for your '/path/to/images' folder of your 'Default Web Site'.

This can be useful in case you do autimaticaly configuration of your IIS with a couple of scripts or BAT/CMD-files.

fycth
  • 3,410
  • 25
  • 37