Saving Images with Javascript

R

Rami A. Kishek

I'm new to js. Please help me figure this out

WANT: Client to save an image to disk pressing a "download" button.
The image name is dynamic, from JS code on another part of the
page
DID: Read somewhere I can use document.execCommand("SaveAs") to do it
This solves it for MSIE, at least. My code is below

PROBLEM: What actually happens with MSIE is:
1) A window pops up promting me to save as.
2) The filename is correct, but the saveas type shown in the
box is limited to "html" and "txt"
3) The "image" that is saved is not an image, but an
HTML file containing the link to the image!! This is
NOT what I want!

PLEASE help. How do I get MSIE to save the actual image instead?
PS With netscape, instead of the saveas dialogue, a window opens with
the image displayed. By clicking <CTRL-S> I can save the actual image.

CODE ----

dld = function(picname, dirpath) {
// This method downloads the slide at the specified resolution.

var newwindow;

obj = dirpath+picname

newWindow = window.open( obj, "SaveImage",
"directories=no,status=no,menubar=no,toolbar=no,resizable=no,top=100000,left=100000,width=0,height=0");
newWindow.document.execCommand('SaveAs', true);
newWindow.close();
return false;
}

example invocation:

<A ID="ss_view" HREF="javascript:dld('pic.jpg','../Photos/')">
<SPAN><img src="../imgs/dld.gif" alt="Download" border="0"></SPAN></A>
 
R

Rami A. Kishek

Thanks. My audience here is very limited. I like to share a personal
family album with friends/ family / etc. The problem is that some
people may want to download a large amount of photos but not everything,
so pressing a button to do it is easier on the fingers than
right-clicking and save as. I also have zipfiles available for those
with mega-throughput networks, but since those files are huge and many
contain many photos that my users would NOT want to download, I like to
give them the other option of downloading one by one.

Of course, I do not intend to over-write files. The solution I began
implementing, with Explorer's document.execCommand('SaveAs', true) opens
up a dialog box asking them where to save, so its safe. Unfortunately,
yes I am married to Explorer with my application, as I use dynamic
tables and (with my limited experience), I found it pretty hard to
change table layouts after the fact with Javascript (e.g. try changing
colSpan for a cell). So IE provides a nice feature called swapNode
which does it. Anyway, luckily my audience is limited so I don't have
to worry about cross-platform compaitibilty (though I myself use Mozilla
on Linux for browsing).

Anyway, to cut a long story short, does anyone have a clue why the
execCommand("SaveAs") is behaving so strangely?
 
V

Vincent van Beveren

Hi,

The problem is that IE hasn't detected what kind of document it is, in
other words, you are too quick.

Something like this should work for IE:

var win;

function check() {
if (win.document.readyState=='complete'){
win.document.execCommand("SaveAs");
win.close();
} else {
window.setTimeout('check();',1000);
}
}

function saveImage() {
win = window.open('prutserprijs.gif','win');
window.setTimeout('check();',1000);
}

good luck,
Vincent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top