Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Function to pop up a save as dialog box

    Hello all -

    Any help would be greatly appreciated, for I am fairly new to JavaScripting. I am trying to create a function that force the download of an image on his/her computer. Right now, with the function I have created, when the user clicks the "Download" link, a "Save As" box opens, but it is trying to save the HTML page, not the actual image. How can i go about reworking my code to save the image, not the HTML page. Here is what i currently have:

    SORRY: HERE'S THE REAL CODE
    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Cadillac Surprise & Delight</title>
    </head>
    
    <body>
    <!--
    
    /* This is the function that opens the  'save as' dialogue box in IE. */
    /* It appears to fail gracefully in mozilla-based browsers but hsa not */
    /* been fully tested */
    -->
    <script language="JavaScript">
    function forceSaveAs (imagename){
    	document.execCommand('SaveAs',null,imagename)
    }
    
    </script>
    
    <div>
    	<div class="thumb_img"><img src="thumb.jpg" border="0"></div>
    	<div class="thumb_txt"><a href="wallpaper.jpg" onclick="forceSaveAs('wallpaper.jpg'); return false">Download</a></div>
    </div>
    Last edited by susoxty; 12-30-2005 at 04:17 PM.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Code:
    <a href="image1.php"><img src="image1.jpg" style="border:0;"></a>
    image1.php
    PHP Code:
    <?php
    // We'll be outputting a .JPG
    header('Content-type: image/jpeg');

    // It will be called image1.jpg
    header('Content-Disposition: attachment; filename="image1.jpg"');
    ?>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Regular Coder feras_wilson's Avatar
    Join Date
    Jul 2005
    Location
    Sweden
    Posts
    129
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Easy

    type this:
    <a href="saveimage()">Save</a>
    <img name="myimg">
    <script>
    function saveimage() {
    myi = window.open()
    myi.document.execCommand("saveas","","myimage.jpg")
    }
    </script>

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,472
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Quote Originally Posted by feras_wilson
    type this:
    <a href="saveimage()">Save</a>
    <img name="myimg">
    <script>
    function saveimage() {
    myi = window.open()
    myi.document.execCommand("saveas","","myimage.jpg")
    }
    </script>
    The request was for a JavaScript solution and the code you provided is JScript which Internet Explorer provides for use on Intranets where it is the only browser being used to display the page. The HTML/PHP solution is the only cross browser solution.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •