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
2008-12-30 03:52:40
+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
2008-12-30 04:10:35
as Craig says, just wrap the image with a hyperlink, that points to a file (with the appropriate headers set) to download.
scunliffe
2008-12-30 04:50:08
Good clarification; I'll add it to the answer.
Craig Stuntz
2008-12-30 13:26:17
I've never heard of application/x-download, but I have heard of application/octet-stream.
R. Bemrose
2008-12-30 13:29:19
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
2008-12-30 14:17:44
+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
2008-12-30 04:17:24