views:

5339

answers:

4

When a user clicks on an image on a web page, I'd like to trigger the browser's Save Image dialog and to let the user save the image on their hard drive. Is there a cross-browser way to do this with jQuery/Javascript?

A: 

I don't imagine so - a lot of the basic browser functionality (eg: Print Preview) isn't available to Javascript.

nickf
+5  A: 

Not precisely, but you can do it by hyperlinking to the img file and setting the content-type and content-disposition headers in the server response. Try, e.g., application/x-download, plus the other headers specified here.

Craig Stuntz
as Craig says, just wrap the image with a hyperlink, that points to a file (with the appropriate headers set) to download.
scunliffe
Good clarification; I'll add it to the answer.
Craig Stuntz
I've never heard of application/x-download, but I have heard of application/octet-stream.
R. Bemrose
From TFA: "[...] to a nonstandard value such as application/x-download. It's very important that this header is something unrecognized by browsers because browsers often try to do something special when they recognize the content type."
Craig Stuntz
+1  A: 

The only thing that comes to my mind is the document.execCommand("SaveAs") of Internet Explorer, you can open a window or use a hidden iframe with the url of your image, and then call it...

Check (with IE of course) this example I've done.

CMS
A: 

have the same question: how to open dialog for save image by click hyperlink just using javascript (not use server code). Anyone have sollution, plz raise it for us, thaks and best regards