473,465 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Saving Images with Javascript

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,re sizable=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>
Jul 23 '05 #1
5 30163
In article <40***************@ebte.umd.edu>,
"Rami A. Kishek" <ra****@ebte.umd.edu> wrote:
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.


Javascript doesn't have any Input/Output functions as part of the
language. You wouldn't want someone overwriting a system file when you
visited a random web site.

If the ctrl-s solution is good enough, why don't you put the image into
it's own window and let the user do the ctrl-s. Also, I assume you know
that the user can right click on the image to save.

To get what you propose to work, you will have to do a proprietary
solution for each web browser. You will miss some.

I do not know the audience for you application, but the typical web user
should realize that the typical web application doesn't implement what
you are proposing. The typical user should know the can right click to
save an image. The typical user won't miss the lack of a special save
image button.

In summary, skip this feature.

Robert
Jul 23 '05 #2
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?

Robert wrote:
Javascript doesn't have any Input/Output functions as part of the
language. You wouldn't want someone overwriting a system file when you
visited a random web site.

If the ctrl-s solution is good enough, why don't you put the image into
it's own window and let the user do the ctrl-s. Also, I assume you know
that the user can right click on the image to save.

To get what you propose to work, you will have to do a proprietary
solution for each web browser. You will miss some.

I do not know the audience for you application, but the typical web user
should realize that the typical web application doesn't implement what
you are proposing. The typical user should know the can right click to
save an image. The typical user won't miss the lack of a special save
image button.

Jul 23 '05 #3
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

Jul 23 '05 #4
In article <40***************@ebte.umd.edu>,
"Rami A. Kishek" <ra********@yahoo.com> wrote:
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.


I wrote up a sample javascript file to facilitate the downloading of
image files. How the browser reacts depends on the mime type. I tried
tgz and gif. I tested this under netscape 7.1 and ie 5.2 on MacOS
10.2.6.

I am not able to get document.execCommand('SaveAs', true) to work on
MacOS, so I am not able to help you there.
Robert

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>download experiment</title>
<script type="text/javascript">
function dld(objurl) {

var newWindow = window.open( objurl, "SaveImage",
"resizable,status=no,menubar=no,toolbar=no");

return false;
}

</script>

</head>

<body>

<p>I do not have a PC zip file, but I'll try a MacOS tgz file.</p>
<a
href="http://sveinbjorn.vefsyn.is/files/software/osxutils-1.4.tgz">
<b>Download Now</b></a>
<br>
<br>
<br>
<p>Let's try to download an image.</p>
<p>To download the image below, click on the image. The image will come
up in a new window. In the new window, press control-s which will
bring up the save diaglog box.</p>
<A ID="ss_view"
onclick="dld('http://image.versiontracker.com/images/dlbug.gif')"
HREF="http://image.versiontracker.com/images/dlbug.gif">
<img src="http://image.versiontracker.com/images/dlbug.gif"
alt="Download" border="0"></A>

</body>
</html>
Jul 23 '05 #5
Thanks so much! Exactly what I was looking for

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

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lovely Angel For You | last post by:
Dear Friends Hope you all doing great. I have this problem. When I try to save any ASP page, I get the message that "The page will not save correctly". Even though I go ahead and when I go...
0
by: Mattias | last post by:
I'm saving pictures on my harddrive from our webcam with GetResponseStream() .. Everything works fine, but after approximately 1h identical images are being saved. I checked the webpage but those...
0
by: Peder Y | last post by:
Say I have several images, a jpg a gif and a bmp. Now, I want to save these files to a single binary file. I store the images in, say, Image img1, img2, img3; 1) How can I retrieve the size...
1
by: Lars Netzel | last post by:
I have heared that one can save images as binary stuff in a database field of an SQL server 2000, is this possible and how do one go about to do it? Is there any restrictions? I'm going to use...
1
by: M Keeton | last post by:
I currently have a picture which is stored in a "System.Drawing.Image" variable and I want to save it as a bitmap file. I have tried 2 different approaches and both give me the following error: ...
0
by: The Confessor | last post by:
I'm relatively new to using FileStreams, so bear with me. I'm trying to use the following code Dim MapGraphicsFileStream As New System.IO.FileStream(MapGraphicsFile, System.IO.FileMode.Create)...
6
by: NutsAboutVB | last post by:
Hello, I am a .NET programmer and I have a JPEG image file (from digital camera) of about 109 KB's in size, when I open it and save it (without making any alterations at all, just going to File...
2
by: carj2ee | last post by:
I trying to write a program that will go to a website and get all the images and then save then on to a local drive. I am using a mshtml.HTMLImg variable and then directcasting the web page to...
6
by: Eddie | last post by:
Hi all, I am displaying a number of reports, and giving the users an option to display them on the web or download them to Excel. If they want the Excel file, I just use the PHP header command...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.