1

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
  • 111
  • 2
  • 5

7 Answers7

1

What you need to do is get a content disposition header into the HTTP response to tell it to treat the image as a file for download, like this one:

Content-disposition: attachment; filename="someimage.png"

I'm afraid I do not know exactly how you would accomplish this on IIS.

In your situation, if IIS failed to provide me with a way of working the header in, I'd probably run the requests through a script that would push out the image data with the header I needed attached.

chaos
  • 7,463
  • 4
  • 33
  • 49
1

You can turn on directory browsing (Directory tab under properties) and add the following header on that directory (HTTP Headers tab)

Custom Header Name: Content-disposition
Custom Header Value: attachment

This will have the unfortunate effect of making the first hit on the folder that would normally display the list of images popup an Open/Save dialog (you can just hit open) and then each image will also receive that same treatment.

Note: this is really handled better through an index.asp|aspx|jsp|php|whatever page that would serve the image and set the headers appropriately - but hey, you asked for an IIS solution.

Goyuix
  • 3,164
  • 5
  • 28
  • 37
  • I can't agree more- this is a code problem not a server problem. It makes no sense to screw with the server to make one directory act differently. – Jim B Jun 24 '09 at 21:21
0

The only way I know of doing this is to try and confuse the browser into thinking the return image file is not an image, and so forcing a download. You could do this using some server side programming to effectively obfuscate the content-type header of the png file sent to the browser, this should then do what you want.

Sam Cogan
  • 38,158
  • 6
  • 77
  • 113
0

Remove the .PNG Mime Type - Instructions here for IIS 7

Helvick
  • 19,579
  • 4
  • 37
  • 55
  • We aren't using IIS 7 unfortunately. – RedWolves Jun 24 '09 at 19:54
  • 1
    Here are the instructions for IIS 6 : http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/cd72c0dc-c5b8-42e4-96c2-b3c656f99ead.mspx?mfr=true – sclarson Jun 24 '09 at 20:08
  • 2
    Of course that means all .png image files would be forced to be downloaded, not just those in the specified folder. – HipCzeck Jun 24 '09 at 20:43
  • You can limit this to a single folder using a virtual directory but having actually tested it now I see that it makes no difference. At least for IE browsing such a page. The Content-Disposition header appears to be required and the only practical way to do this is programatically (as others have pointed out) in order to build the the necessary html that includes the correct header for each file. – Helvick Jun 24 '09 at 23:04
0

The general rule is that if the browser gets a content type it does not have a handler for it will open the 'save-as' dialog.

If you were using Apache I would suggest placing a .htaccess file only in that folder and only overrule it for png files in only that directory.

I.e. something like this:

AddType application/bin png

For IIS I do not know where you can configure that without breaking all PNG files.

One possible solution is to install an Apache on a system (perhaps the same system as the IIS) and let IIS proxy the requests for this images folder to the Apache and use the trick with the htaccess to force downloading them.

Niels Basjes
  • 2,176
  • 3
  • 18
  • 26
0

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
  • 111
  • 2
  • 5
-1

An easy way would be to wrap the PNG file in a PHP file. You could then use URL re-writing to pick up *.png files and translate them to the PHP file (e.g. downloadpng.php?file=image.png):

<?php

header ('Content-disposition: attachment; filename="'.$_GET['file'].'"')
include ($_GET['file']);

?>

My PHP is exceptionally rusty, but something along those lines would suffice. Maybe best to ask over at Stack Overflow for correct syntax.

If you don't know what URL Re-writing is, there's plenty of places you can google to find out what it does, but in a nutshell it takes a browser URL and re-interprets it to something else before it reaches IIS, so that the user sees one URL but IIS sees another (which is how you can force .png files to go through a PHP script instead).

Mark Henderson
  • 68,316
  • 31
  • 175
  • 255