0

I am trying to use AJAX & PHP Function for downloading a file. my goal is to use AJAX for sending variables from Datatable to PHP function. and PHP function will search for the file in my storage and download it.

It's not working. when I using the URL I can download the file. but when I trigger it with AJAX it's not working.

I want to create a PHP function that will receive file name and generate the download link for an HTML button. (which displayed in a data table)

Ajax:

       //download button
    $('#files').on('click', 'button#download',function (ref) {
        var data = table.row($(this).parents('tr')).data();
        var file_name=data.filename;
        ref.preventDefault();
      //  alert(data.filename);
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $.ajax({
            type: 'POST',
            datatype:'binary',
            url: "/download?"+data.filename,
            success: function(result){
                console.log(result);
            }
        });
    });

PHP:

    public function getDownload(){
    $tmpfile='1520001503b1.png';
   $sd= DB::table('files')
        ->where('filename',$tmpfile)
        ->get();


    $myFile = public_path("uploads/1520001503b1.png");
    $headers = ['Content-Type: application/png'];
    $newName = 'bla'.time().'.png';


    return response()->download($myFile, $newName, $headers);

}

route:

Route::match(['get', 'post'], '/download','FilesController@getDownload');
bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • 1
    Okay? So what's the question? – Jonnix Mar 04 '18 at 09:38
  • It's not working. when I using the URL I can download the file. but when I trigger it with AJAX it's not working. – שגיא שלום Mar 04 '18 at 10:14
  • 1
    AJAX here doesn't make sense. Just use the link and the browser will deal with downloading it if the response headers of the download link have been set properly. – Jonnix Mar 04 '18 at 10:24
  • Possible duplicate of [download file using an ajax request](https://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request) – Jonnix Mar 04 '18 at 10:25
  • So the answer could be that you must let the user choose downloading by clicking on the file url. Browser wont let another way for security reasons – Luis Gar Mar 04 '18 at 10:30
  • This looks like a lot of complex JS for something you could just as easily implement with a regular link. – Quentin Mar 04 '18 at 11:08
  • not all of the files stored exactly the same location, so I want to create a function that will receive file name (key) and generate the right download. – שגיא שלום Mar 04 '18 at 11:45
  • I want to create a PHP function that will receive file name and generate the download link for an HTML button. – שגיא שלום Mar 04 '18 at 12:05
  • How about createObjectURL from blob on clientside, you send base64? Better solution ofc is to create your own download handler, which has an URL. You just generate some id-s in DB and redirect with that ID towards aforementioned URL. – Mati Mar 04 '18 at 13:05

1 Answers1

0

as far as I know You can't download the files from a direct ajax request instead you could return a dynamic download url to the ajax response and just trigger browser location change as below in ajax success function.

location.href = 'download.php';
fayis003
  • 680
  • 4
  • 10