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?
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.
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.
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.
Remove the .PNG Mime Type - Instructions here for IIS 7
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.
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.
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).