How to download .JPG file?

Discussion in 'HTML & Website Design' started by creativewebmaster, Jun 27, 2013.

  1. #1
    Dear Folks,

    I have simple HTML page and there are one button called "Download". Once user click on that button then user can download the .jpg file. I do not want to open in new window or same window. I just want to know code for that.

    Please let me.

    Thanks,
     
    creativewebmaster, Jun 27, 2013 IP
  2. BackSlashDesigns

    BackSlashDesigns Active Member

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    60
    #2
    I am not sure that this is possible because usually your browser detects that it is a image and opens it up in the browser. What you could do is put the .jpg file in a zip file and provide a link to the zip file. So when someone clicks on the link they download the zip file and they can easily extract the image from there.
     
    BackSlashDesigns, Jun 27, 2013 IP
  3. creativewebmaster

    creativewebmaster Active Member

    Messages:
    654
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    78
    #3
    Dear BackSlashDesigns,

    Thanks for your feedback but, I will not need .zip file to download. Actually in my page have 1000 photos and i want to download them using click on each button for each images.

    Thanks
     
    creativewebmaster, Jun 27, 2013 IP
  4. BackSlashDesigns

    BackSlashDesigns Active Member

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    60
    #4
    I done a quick Google search and most of them say you have to use JavaScript. Look at this website:
    http://stackoverflow.com/questions/10473932/browser-html-force-download-of-image-from-src-dataimage-jpegbase64
     
    BackSlashDesigns, Jun 27, 2013 IP
  5. GMF

    GMF Well-Known Member

    Messages:
    855
    Likes Received:
    113
    Best Answers:
    19
    Trophy Points:
    145
    #5
    Quick and dirty

    
    <a href="./image/test.png" download="Image"><button>Download!</button></a>
    
    HTML:
    Note: the download attribute is not supported by all browsers. Here is a list the ones that support it

    http://caniuse.com/#feat=download
    Code (markup):
     
    GMF, Jun 27, 2013 IP
  6. abhijitnumber1

    abhijitnumber1 Member

    Messages:
    19
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    38
    #6
    If you can use php the this is the code

    $file = '[B]Your file path[/B]';
     
     
    $known_mime_types=array(
        "gif" => "image/gif",
        "png" => "image/png",
        "jpeg"=> "image/jpg",
        "jpg" =>  "image/jpg",
    );//file type allow to download
     
     
    if (file_exists($file) && array_key_exists($file_extension, $known_mime_types))//check for [B]file exists[/B] & check for [B]file type[/B]
    {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
    PHP:
     
    abhijitnumber1, Jul 3, 2013 IP