Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onClick show save as box? 1

Status
Not open for further replies.

Zac5

Programmer
Jan 30, 2003
75
0
0
US
Hi,

I need to display a button on a page which when clicked prompts the user to save a particular image on their machine. I know that you can do this by right clicking your mouse button on an image, however, in this case the image will not be displayed on the screen, instead a perl script will pass a file name. Is this possible usin javascript - does anyone have any ideas please?

Thanks

Zac
 
The only way you can bring up a "save" dialog is with a file that cannot be opened/read by the browser. So the one sure way to get a "save" dialog is to make your file a zip file.

If you are merely passing the filename to another script, perhaps it (the filename) could be identified as a zip on the web page, but converted to jpg by your script.

I don't really understand if you want the picture displayed or not, so if these comments don't help, you might post a little more information.

Hope that helps.
 
Try this:


function saveImageAs (imgOrURL) {
if (typeof imgOrURL == 'object')
imgOrURL = imgOrURL.src;
window.win = open (imgOrURL);
setTimeout('win.document.execCommand("SaveAs")', 500);
}
<A HREF=&quot;javascript: void 0&quot;
ONCLICK=&quot;saveImageAs(document.anImage); return false&quot;
>save image</A>
<IMG NAME=&quot;anImage&quot; SRC=&quot;whatever.gif&quot;>
 
Adam,

Your solution has almost got me there, the only problem is that I don't really want to be opening the image, however. I will play around with the script you have given and see what I can achieve, in the meantime if you have any further bright ideas I would be very happy to know, thanks a lot for you help.

Zac
 
How about something like this:

<script>
function saveImageAs (imgOrURL) {
if (typeof imgOrURL == 'object')
imgOrURL = imgOrURL.src;
document.frames['myiframe'].location.href= imgOrURL;
setTimeout('document.frames['myiframe'].document.execCommand(&quot;SaveAs&quot;)', 500);
}
</script>
<iframe name=&quot;myiframe&quot; style=&quot;display:none&quot;></iframe>
<A HREF=&quot;&quot;
ONCLICK=&quot;saveImageAs(document.anImage); return false&quot;
>save image</A>
<IMG NAME=&quot;anImage&quot; SRC=&quot;whatever.gif&quot;>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top