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

  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
    #2
    1. Advertising

  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
    Vincent van Beveren, Jun 3, 2004
    #3
  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
    #4
    1. Advertising

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
    Replies:
    1
    Views:
    373
    vMike
    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
    Replies:
    2
    Views:
    40,360
    =?Utf-8?B?VGltOjouLg==?=
    May 18, 2004
  3. John Kandell
    Replies:
    4
    Views:
    4,141
    eeebop
    Dec 10, 2004
  4. Luis Esteban Valencia
    Replies:
    0
    Views:
    2,490
    Luis Esteban Valencia
    Jan 6, 2005
  5. Lovely Angel For You

    Saving Images While Saving ASP Pages !

    Lovely Angel For You, Oct 2, 2003, in forum: ASP General
    Replies:
    1
    Views:
    194
    Curt_C [MVP]
    Oct 3, 2003
Loading...

Share This Page