1

I am trying to get my php script to download an image which the name of is being sent in from an ajax function in my javascript, and I think I'm using all the right headers and then I'm using readfile() to download the image, and when I preview the php when it runs with the chrome developer tools (ctrl+shift+i-> network -> XHR -> preview) all I get is a bunch of gibberish binary and my image doesn't download. Does anyone know why this is happening/how to fix it?

Here's my ajax which is just in a function that runs when a "download" button is clicked

$.ajax({

    type: "POST",
    url: 'requestAJAX.php',
    data: {request: request, download: imageName},
    success: function(){
        console.log("Request Completed: " + request);
    } // success   
}); 

and here's my "requestAJAX.php"

<?php

switch ($_POST["request"]) {
case "download":

    // Get parameters
    $file = $_POST["download"]; 
    $filepath = "UploadedImages/" . $file;

    if(file_exists($filepath)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.$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($filepath));
        ob_clean();
        flush();

        // download 
        readfile($filepath);
        exit;
    } // if
    break; 
default: 
    echo "No request was made"; 
}
?>

and this is what I'm getting when I preview (not sure how it will help)

ÿØÿàJFIFHHÿâ@ICC_PROFILE0appl mntrRGB XYZ ÙacspAPPLapplöÖÓ-appldscmòdescüogXYZlwtptrXYZbXYZ¨rTRC¼cprtÌ8chad,gTRC¼bTRC¼mlucenUS&~esES&daDK.êdeDE,¨fiFI(ÜfrFU(*itIT(VnlNL(nbNO&ptBR&svSE&jaJPRkoKR@zhTWlzhCNÔruRU"¤plPL,ÆYleinen RGB-profiiliGenerisk RGB-profilProfil Générique RVBN, RGB 0×0í0Õ0¡0¤0ëu( RGB r_icÏðPerfil RGB GenéricoAllgemeines RGB-Profilfn RGB cÏðeNöGenerel RGB-beskrivelseAlgemeen RGB-profielÇ|¼ RGB Õ¸\ÓÇ|Profilo RGB GenericoGeneric RGB Profile1I89 ?@>D8;L RGBUniwersalny profil RGBdescGeneric RGB ProfileGeneric RGB ProfileXYZ Zu¬s4XYZ óRÏXYZ tM=îÐXYZ (¸6curvÍtextCopyright 2007 Apple Inc., all rights reserved.sf32BÞÿÿó&ýÿÿû¢ÿÿý£ÜÀlÿáExifMM*JR(iZHH é ¡ÿÛC  

Thanks in advance for any assistance!

Matthew Smith
  • 45
  • 3
  • 10
  • does [this post](https://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post) help you? So far what I see from your code is in a good way, but you ask for binary data (`header('Content-Transfer-Encoding: binary');`) and you do nothing with it in the success function. It won't turn from binary data to a download by itself client-side.. – Kaddath Jan 03 '20 at 08:44
  • why to use so long process just to download.use download prop in html itself: here is the example: – Ritesh Dhoke Jan 03 '20 at 11:49
  • Everything looks correct. JPEG pictures **are** binary and you're instructing the browser to handle it as arbitrary binary data (`Content-Type: application/octet-stream`). – Álvaro González Jan 03 '20 at 11:50

0 Answers0