Web Analytics Made Easy -
StatCounter invoking "Save Target As" dynamically on an image - CodingForum

Announcement

Collapse
No announcement yet.

invoking "Save Target As" dynamically on an image

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • invoking "Save Target As" dynamically on an image

    Hello

    I was wondering whether it is possible to invoke the "Save Target As" option (the one accessed via right-clicking a file) on an image in a page.
    That is, if the user clicks on a link/button the behaviour should be as if the user right-clicked an image and chose "Save Target As" (on IE of course), i.e. open the save file dialog.

    thanks

  • #2
    Check out these links:
    Founded in 1997, DEVShed is the perfect place for web developers to learn, share their work, and build upon the ideas of others.

    Founded in 1997, DEVShed is the perfect place for web developers to learn, share their work, and build upon the ideas of others.


    Hope that helps,
    Sadiq.

    Comment


    • #3
      You can't do it in javascript. Probably possible with IE using ActiveX commands. The better alternative is to force the Download dialog box to appear. You can do it using a server-side language. The server-side language reads the binary data of the file and sends it to the browser with appropriate HTTP headers that will force the browser to invoke the Open/Download dialog box.
      Glenn
      vBulletin Mods That Rock!

      Comment


      • #4
        As I tould you in other forum... There might be a javascript solution either. You may try inserting your image directly in an iframe or a frame. Now you can fire the exec.Command('SaveAs') upon the iframe, as this that method can be executed only upon the whole document, not upon the inner objects.
        KOR
        Offshore programming
        -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

        Comment


        • #5
          I want to be able to do this without displaying the image.
          I want to have a link saying something like: "download image". Clicking the link will open the save as dialog for a specific image on the file system

          Comment


          • #6
            Ok, try this (see the attachment also, for full example):

            PHP Code:
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
            <
            html>
            <
            head>
            <
            script>
            function 
            saveAs(a){
            document.getElementById('ifr').setAttribute('src',a);
            top.frames[0].location.href.split('/');
            zz z[z.length-1];
            if(
            zz == a){top.frames[0].document.execCommand('SaveAs');clearTimeout(y)}
            else{
            setTimeout('saveAs(\''+a+'\')',1)}
            }
            </
            script>
            </
            head>
            <
            body>
            <
            a href="#" onclick="saveAs('00.jpg');return false">Save image 00.jpg</a><br>
            <
            a href="#" onclick="saveAs('01.jpg');return false">Save image 01.jpg</a><br>
            <
            a href="#" onclick="saveAs('02.jpg');return false">Save image 02.jpg</a><br>
            <
            iframe id="ifr" src="" style="visibility:hidden"></iframe>
            </
            body>
            </
            html
            Attached Files
            KOR
            Offshore programming
            -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

            Comment


            • #7
              thanks this works very well

              the only thing that concerns me is the fact that the page continues loading in the background. What would happen if the user clicks on the link - the dialog opens and then the user does nothing. What will happen to the page on the timeout?
              is there a way to stop the page from loading?

              thanks for the help

              Comment


              • #8
                Theretically there is a clearTimeout which should stop the loop if condition is fulfiled.

                I had to do that because, and this is weird for me also (I shall put a thread with this issue) after changing dynamically the src attribute, location.href finds the new location only after the second firing of the function... that is bizzare, indeed.
                KOR
                Offshore programming
                -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

                Comment


                • #9
                  try clear the Timeout before Save As. Try this new line

                  if(zz == a){clearTimeout(y);top.frames[0].document.execCommand('SaveAs')}

                  instead of

                  if(zz == a){top.frames[0].document.execCommand('SaveAs');clearTimeout(y)}
                  KOR
                  Offshore programming
                  -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

                  Comment


                  • #10
                    no, it doesn't work

                    by the way, where does 'y' gets defined in the first if statement.
                    It is only defined in the else part so I am not sure how the browser doesn''t complain

                    Comment


                    • #11
                      Why are you trying to do this in javascript? execCommand is only gonna work in Windows IE.

                      You can do it in PHP - http://www.sitepoint.com/forums/showthread.php?t=156577
                      "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

                      Comment


                      • #12
                        no, it doesn't work
                        It works with no problem here (IE6 on XP). That is true that first time when click, and confirm or cancel the SaveAs dialog, for some time appears a peculiar "Downloading from site..." on status bar... But the page is not reloaded and I confess I don't understand what is downloaded there. But that status bar message dissapears soon, and later never appears.

                        At least for me, locally nothing notable happens.
                        KOR
                        Offshore programming
                        -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

                        Comment

                        Working...
                        X