mercredi 6 novembre 2019

Unable to download csv file in Laravel

I've been trying to download the selected data in csv format but I'm not able to. Here's what I have:

public function download_csv($request, $columns, $filename){

    $downloads = $this->search($request);
    $handle = fopen($filename, 'w+');

    fputcsv($handle, $columns);

    foreach($downloads as $download) {
        fputcsv($handle, array(
            $download['room_list']['room_name'],
            $download['room_list']['room_code'],
            $download['reservation_id'],
            $download['registered_name'],
            $download['number_guest'],
        ));
    }

    fclose($handle);

    $headers = array(
        'Content-Type' => 'text/csv',
    );

    return response()->download($filename, $filename, $headers);
}

The file is created but is not downloaded. By the way, I'm using ajax to send parameters to my controller and I think it's the one affecting the process.

$(document).on('click','.download-csv', function(){
    let account_name  =  $('#account_name').val();
    //more parameters

    $.get('/download_csv', {
        account_name: account_name,
       //more parameters
    });
});

How can I surpass this one? Is there any better suggestions?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire