Force PNG to download instead of opening in browser with IIS

6,303

Solution 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.

Solution 2

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.

Solution 3

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.

Share:
6,303

Related videos on Youtube

RedWolves
Author by

RedWolves

Developer Advocate for Atlassian's Ecosystem platform and Atlassian Marketplace Follow me on twitter @RedWolves

Updated on September 18, 2022

Comments

  • RedWolves
    RedWolves over 1 year

    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
    RedWolves almost 15 years
    We aren't using IIS 7 unfortunately.
  • Dave Klotz
    Dave Klotz almost 15 years
  • HipCzeck
    HipCzeck almost 15 years
    Of course that means all .png image files would be forced to be downloaded, not just those in the specified folder.
  • raja
    raja almost 15 years
    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.
  • Helvick
    Helvick almost 15 years
    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.