Javascript to simulate "right click on image -> save image as"

user1643156 picture user1643156 · Nov 29, 2012 · Viewed 7.8k times · Source

OK, there has been an extact duplicate question as this one, but it's been 2 years away, and there's only one answer which was not really a solution.

So, I have an image (thumbnail), right below it there are 2 buttons: View and Download.

View will open the image in browser (cached). And the Download button will open the Save Image dialog.

Right now I'm using PHP's header Content-Disposition: attachment; for the download button.

Generally, visitors will first hit the View button to preview (thumbnail is not a solution since details and quality need to be verified), especially for my own app.

Now, I don't really want the file to be read again through PHP since it takes time. And for a better user experience, is it possible to simulate "right click on image -> save image as" so the download dialog pops up, and visitors only need one single click to download?

Answer

bfavaretto picture bfavaretto · Nov 29, 2012

No, it's not possible. You must fire a new request and force the download using http headers, as you're currently doing.