views:

40

answers:

2

We want to allow a user to download a picture via a button from our website. We've got the basic download working ok (it saves it to the disk) but we do it by opening up a new window...

window.open('mypicture', '_blank');

Unfortunately this creates a new tab in Chrome which does not go away. We don't have the most sophisticated users and they might become confused if the screen suddenly goes blank.

We tried it with...

location.href='mypicture'

This works great in IE, but in Chrome it just ignores the download completely.

Is there any other way to force download the picture without opening a new window?

+2  A: 

Ideally, you should send http header in order to prompt the picture to be downloaded like

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

..so you should point your link to a PHP script (or whatever), which sends a picture with proper headers.

lashtal
As I mentioned, I've got the file to download ok. What I'm trying to do is get it to download without having to open a new browser window.
Brian
Using the Content-Disposition HTTP header is going to be the only way. You could write a PHP script whose sole purpose is to write the header and then print the contents of the image file. Either that, or just direct the user to right-click and save the image manually.
Christopher Parker
I wonder if you could do something fancy with httpd.conf (or possibly even .htaccess) to force the inclusion of this header for all image files in a directory or even just one specific image file...
Christopher Parker
As I've stated, this is not my problem. I've already done this. What I want is to be able to download the photo using a <button> tag without having to open another window. I've resolved it by just using a <a> link for now (see my answer), but unfortunately this is not how the UI was designed.
Brian
A: 

Perhaps this is a bug in Chrome (FF too)? I just tried it with a link and it works fine.

<a href="mypicture">Download</a>
Brian

related questions