Saving Images with Javascript

Discussion in 'Javascript' started by Rami A. Kishek, Jun 3, 2004.

  1. 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
    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 = obj, "SaveImage",
    newWindow.document.execCommand('SaveAs', true);
    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>
    Rami A. Kishek, Jun 3, 2004
    1. Advertisements

  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.
    Rami A. Kishek, Jun 3, 2004
    1. Advertisements

  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'){
    } else {

    function saveImage() {
    win ='prutserprijs.gif','win');

    good luck,
    Vincent van Beveren, Jun 3, 2004
  4. 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.
    Rami A. Kishek, Jun 4, 2004
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. toma

    Saving resized Images

    toma, Nov 25, 2003, in forum: ASP .Net
    Nov 25, 2003
  2. =?Utf-8?B?VGltOjouLg==?=

    Error Saving Images: A generic error occurred in GDI+.

    =?Utf-8?B?VGltOjouLg==?=, May 18, 2004, in forum: ASP .Net
    May 18, 2004
  3. William Ortenberg

    Saving images

    William Ortenberg, Aug 6, 2004, in forum: ASP .Net
    Kevin Spencer
    Aug 6, 2004
  4. John Kandell
    Dec 10, 2004
  5. Luis Esteban Valencia
    Luis Esteban Valencia
    Jan 6, 2005
  6. Lars Netzel
    Steve C. Orr [MVP, MCSD]
    Aug 2, 2005
  7. Fred

    preventing images saving

    Fred, Jan 24, 2006, in forum: ASP .Net
    Brian Cryer
    Jan 26, 2006
  8. Lovely Angel For You

    Saving Images While Saving ASP Pages !

    Lovely Angel For You, Oct 2, 2003, in forum: ASP General
    Curt_C [MVP]
    Oct 3, 2003