Linking Image for download in HTML

Discussion in 'HTML & Website Design' started by pratik, Oct 21, 2009.

  1. #1
    Hi there,

    I am making a link which when clicked should download a jpeg file. But the normal linking tags makes the jpeg to open in the browser itself. I want that it should prompt the user to save jpeg as it does for all other files.

    Any ideas how can I give a link to download jpeg file instead of opening it?


    regards
    pratik
     
    pratik, Oct 21, 2009 IP
  2. Pyrokinetic

    Pyrokinetic Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you could probably accomplish this with php, use the php include(); function to include the jpeg, and in php set the MIME type to something that would try to get downloaded...

    I don't know the specifics right now, if you're interested, look it up on the php website.
     
    Pyrokinetic, Oct 22, 2009 IP
  3. rogan4567

    rogan4567 Active Member

    Messages:
    103
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    55
    #3
    I'm not sure how to do it in php, but this works in perl -- maybe it's useful to you:


    
    $ImageLink = "http://path.to.com/$filename.jpg";
    
    print "Content-Type: application/octet-stream\n"; 
    print "Content-Disposition:attachment;filename=$filename.jpg\n";
    
    print "Location: $ImageLink\n\n";
    exit;
    
    
    Code (markup):
    Try searching for 'force file download php' or something in Google -- I think I ultimately had to work with a php script to come up with the above. It was years ago, though... so I can't remember.
     
    rogan4567, Oct 22, 2009 IP
  4. Bex7175

    Bex7175 Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi there, I believe that this can be done using the header tag in PHP with code similar to the following:

    <?php
    header('Content-Type: image/jpeg');
    header('Content-Length: 1234');
    header('Content-Disposition: attachment;filename="test.jpg"');
    $fp=fopen('test.jpg','r');
    fpassthru($fp);
    fclose($fp); 
    ?>
    Code (markup):
    Where Content-Length should be the size of the filename.

    Hopefully that will work. :)

    Bex
     
    Bex7175, Oct 22, 2009 IP
  5. seomanEP

    seomanEP Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Some users may know that rightclicking on an image allows them to save it. If not you could tell them to right click on the link and select save target as. Otherwise, the PHP methods posted above may be the way to go.
     
    seomanEP, Oct 22, 2009 IP
  6. pratik

    pratik Notable Member

    Messages:
    2,308
    Likes Received:
    114
    Best Answers:
    0
    Trophy Points:
    200
    #6
    Thanks of the response guys..
    but its an ASP page that I am working on.. so I guess that PHP code won't work :(

    Is there any way of doing it by simple HTML or ASP.
     
    pratik, Oct 22, 2009 IP
  7. Clive

    Clive Web Developer

    Messages:
    4,507
    Likes Received:
    297
    Best Answers:
    0
    Trophy Points:
    250
    #7
    Effectively forcing the download of a known MIME type is a problem since browsers will open the image rather than offer a download option by rule.
    Best way in my opinion is to archive the image and upload it as a .zip file. That will surely do the trick.
     
    Clive, Oct 22, 2009 IP