Javascript for download dialof for images ?

Discussion in 'JavaScript' started by QubeSys Technologies, Sep 9, 2011.

  1. #1
    Hi..
    We have href= linked to image paths.

    We dont want the clicks to open the image on the window.

    We want it to prompt for download, or save as for the users.

    Whats the trick ?
     
    QubeSys Technologies, Sep 9, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    Don't use an <a> tag - that's for opening that link. Or have more intelligent users, who know to right-click the link and save the content.
     
    Rukbat, Sep 9, 2011 IP
  3. QubeSys Technologies

    QubeSys Technologies Active Member

    Messages:
    204
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    Sorry, we dont have intelligent audience.
     
    QubeSys Technologies, Sep 9, 2011 IP
  4. babushkyn

    babushkyn Member

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    2
    Trophy Points:
    26
    #4
    I don't think you can do much with JS, perhaps a PHP "proxy" script with certain headers would do that.
     
    babushkyn, Sep 9, 2011 IP
  5. Jan Novak

    Jan Novak Peon

    Messages:
    121
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Try to do this via webserver configuration instead of javascript.

    http://drupal.org/node/417866

    Btw. If you need JS solution: I believe it is possible to register javascript function which forbids opening an image and which creates hidden iframe with src="yourdomain/downloadfile.php?file=image1" and PHP (or another server side language) will send it to browser with HTTP header Content-Disposition attachment...
     
    Jan Novak, Sep 10, 2011 IP
  6. hotnoob

    hotnoob Member

    Messages:
    96
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    28
    #6
    simply return the image with the header information for it to be downloaded.
    here: http://php.net/manual/en/function.header.php


    i honestly hate to give out free scripts that can be used as is, because it doesn't guarantee that you will learn something.
    so promise me that you will read it over, and not just copy and paste it.
    
    <?php
    
    
    $file = 'path/to/image/file.jpg';
    if (file_exists($file))
       {
          header('Content-Description: File Transfer');
          header('Content-Type: download/jpg');
          header('Content-Disposition: attachment; filename='.basename($file));
          header('Content-Transfer-Encoding: binary');
          header('Expires: 0');
          header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
          header('Pragma: public');
          header('Content-Length: ' . filesize($file));
          ob_clean();
          flush();
          readfile($file);
          exit;
        }
        else
        {
          echo 'Error: file does not exist';
        }
    ?>
    
    Code (markup):
     
    hotnoob, Sep 11, 2011 IP
  7. Slincon

    Slincon Well-Known Member

    Messages:
    1,319
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    180
    #7
    Yep - as hotnoob said, you have to run it through some sort of server side script, which can prompt the file as a download.

    The other alternative is creating an htaccess file or configuring your server to treat certain filetypes as ocet-streams, so that the user is prompted to download it.
     
    Slincon, Sep 11, 2011 IP