1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

"Save Image As" triggered by Javascript - HOW?

Discussion in 'JavaScript' started by jkolarov, Feb 12, 2008.

  1. #1
    Hello,
    I was trying to find a solution to my problem, which is:

    How to trigger the "Save Image As" dialog window to open using Javascript?

    My problem is that the files that are going to be saved are dynamically created images, and I'm trying to avoid "right click">>"save image as"

    I searched the web, but still no working solution...I will be happy to hear what the DP members think about this.

    Thanks in advance.
     
    jkolarov, Feb 12, 2008 IP
  2. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    MMJ, Feb 13, 2008 IP
  3. jkolarov

    jkolarov Peon

    Messages:
    109
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Please read carefully, I described what I'm trying to achieve and what to avoid.

    So, as I thought - there is no working solution, and I guess I should go the old fashion way - changing the headers with PHP...
     
    jkolarov, Feb 14, 2008 IP
  4. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you are trying to save images than there is a way to harvest them than download them. :rolleyes:
     
    MMJ, Feb 14, 2008 IP
  5. jkolarov

    jkolarov Peon

    Messages:
    109
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok imagine 1 html page with 1 image on it. How to save the image without having the user right click on it....?
     
    jkolarov, Feb 14, 2008 IP
  6. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You can write a simple php script that retrieves the page than uses a regexp to retrieve the img src.

    Post in the PHP forum if you want to look deeper into this solution.
     
    MMJ, Feb 14, 2008 IP
  7. jkolarov

    jkolarov Peon

    Messages:
    109
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hey MMJ,

    I know how to do this with php, thanks for the not so accurate advice anyway.

    I'm trying to find out how can you do this with Javascript, if this is possible of course.
     
    jkolarov, Feb 14, 2008 IP
  8. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You must have a reason for needing javascript.

    That is why I asked you in my first post; What is it you are trying to do?
     
    MMJ, Feb 14, 2008 IP
  9. jkolarov

    jkolarov Peon

    Messages:
    109
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Ok, nevermind
     
    jkolarov, Feb 15, 2008 IP
  10. lapoo

    lapoo Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Yes I need the same solution.
    Jkolarov I've the same problem.
    I've Found this script but It seem not working

    <script>
    function saveImageAs (imgOrURL) {
    if (typeof imgOrURL == ‘object’)
    imgOrURL = imgOrURL.src;
    window.win = open (imgOrURL);
    setTimeout(’win.document.execCommand(”SaveAs”)’, 500);
    }
    </script>
    <input name="Salva Imagine" type="button" onClick="saveImageAs (document.getElementByID('embedImage'))" value="Salva"  />
    <a href="#" ONCLICK="saveImageAs(document.getElementById('embedImage')); return false" >save image</a>
    
    <img id="embedImage" src="../apache_pb2.gif" width="259" height="32" >
    Code (markup):
    I'm totally newbie with JAVA so someone could explain how to use it?
     
    lapoo, Mar 2, 2008 IP
  11. lapoo

    lapoo Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I'v Founded IT ahahaahahhaaha


    
    </head>
    <script>
    
     function saveImageAs (imgOrURL) {
        if (typeof imgOrURL == 'object')
          imgOrURL = imgOrURL.src;
        window.win = open (imgOrURL);
        setTimeout('win.document.execCommand("SaveAs")', 500);
      }
    </script>
    <body>
    
      <A HREF="javascript: void 0"
         ONCLICK="saveImageAs(document.anImage); return false"
      >save image</A>
      <IMG NAME="anImage" SRC="../apache_pb2.gif">
    </body>
    
    Code (markup):
    Unfortunely It work only with IE not FireFox
     
    lapoo, Mar 2, 2008 IP
  12. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #12
    As I said in my first post.
     
    MMJ, Mar 4, 2008 IP
  13. jkolarov

    jkolarov Peon

    Messages:
    109
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Yep, there isn't seem to be working solution...so we will do it the old-fashioned way...

    at least for now :)
     
    jkolarov, Mar 4, 2008 IP
  14. deepaseinivasan

    deepaseinivasan Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Hi
    I wanted to pop up a save as dialog box to prompt the user to save the charts on my webpage to be saved as an image in his/her machine.
    The below code works fine,but the new window opens where actually the saving of data is taking place before the pop up of save as box.
    How to avoid the new window opening
    Below is the code


    <script type = "text/javascript">

    function saveImageAs (imgOrURL) {
    if (typeof imgOrURL == 'object')
    imgOrURL = imgOrURL.src;
    window.win = open (imgOrURL);
    setTimeout('win.document.execCommand("SaveAs")', 500);

    }
    </script>

    <img name="anImage" src="../ProductBacklog/ChartImages/chart.jpeg" visible = "false" style="width: 1px"/>
     
    deepaseinivasan, Sep 16, 2008 IP
  15. deepaseinivasan

    deepaseinivasan Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
     
    deepaseinivasan, Sep 16, 2008 IP
  16. deepaseinivasan

    deepaseinivasan Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    <script type = "text/javascript">

    function saveImageAs (imgOrURL) {
    if (typeof imgOrURL == 'object')
    imgOrURL = imgOrURL.src;
    window.win = open (imgOrURL);
    setTimeout('win.document.execCommand("SaveAs")', 500);

    }
    </script>
    On an imagebutton I am calling the above script <ClientSideEvents Click='javascript:saveImageAs(document.anImage)' />

    <img name="anImage" src="../ProductBacklog/ChartImages/chart.jpeg" visible = "false" style="width: 1px"/>

    The above location is the place where charts are getting generated dynamicallyu based on the project chosen from dropdown.
    oN CLICK i AM GETTIN A NEW WINDOW OPENED with address http...ProductBacklog/ChartImages.
    The chart is in aspx page in ProductBacklog folder.
    How to avoid this page getting opened ,since this opens up and then the above script pops up Save AS DIALOG BOX.
    Please any help would be appreciated.
     
    deepaseinivasan, Sep 17, 2008 IP
  17. geertj

    geertj Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    The work-around avoiding a new browser window opening is as follows:

    The save-as script in the header:

    <script>
    function SaveFile(fname){
    img.document.execCommand('saveas', null ,fname)
    }
    </script>

    Now the trick is to have the image already opened as a new page.
    To achieve this we put it in an iframe with the image filename as SRC:

    <iframe id="img" src="myimage.jpg" width="(image width + 20)px" height="(image height + 25)px" scrolling="no" frameborder="0px"></iframe>

    On the original page we now call the save-as script:

    <button onclick="SaveFile('myimage.jpg');">save as</button>

    Does not work in FireFox though.....
     
    geertj, Feb 23, 2009 IP
  18. geertj

    geertj Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    The script looks better like this:

    <script>
    function SaveFile(fname){
    document.getElementById('img').contentWindow.document.execCommand('saveas', null ,fname);
    }
    </script>
     
    geertj, Feb 23, 2009 IP
  19. rsrikanth05

    rsrikanth05 Well-Known Member

    Messages:
    1,362
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    190
    #19
    That's not much of a problem, seing that IE has a 70% market share.
     
    rsrikanth05, Feb 23, 2009 IP
  20. airportparking

    airportparking Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Nice post...Thank you very much!!......[​IMG]
     
    airportparking, Sep 15, 2010 IP