lundi 26 octobre 2015

File is not being downloaded on Laravel5 using Response

I use Laravel 5.1.

A jQuery ajax call is made like that:

$('#export_selected').click(function(){
                var checked = $('.select_rows:checked');
                var ids     = [];
                $.each(checked, function(index, value){
                    ids.push(value.id);
                })
                $.ajax({
                    method : "POST",
                    url    : "{{URL::to('/spot/exportTable')}}",
                    data   : {ids:ids}
                });
            });

And then the php method is defined that way:

public function exportTable(Request $req) {
            $spots = array_flatten($req->all());
            $res   = Spot::whereIn('id', $spots)->get();

            Excel::create('Spots', function($excel) use($res) {
                $excel->setTitle('Title goes here');
                $excel->setCreator('Creator Goes Here')->setCompany('Company Goes Here');
                $excel->sheet('Excel sheet', function($sheet) use($res) {
                    $sheet->setOrientation('landscape');
                    $sheet->fromArray($res);
                });
            })->store('csv', storage_path('/exports', true));

            $file    = base_path() . '/storage/exports/Spots.csv';
            $headers = ['Content-Type: application/csv',  'Content Description: File Transfer', 'Cache-Control: must-revalidate, post-check=0, pre-check=0'];
            return response()->download($file, 'Spots.csv' , $headers);
    }

Chrome developer console prints the results as raw lines.

The file is successfully exported and created in the disk.

The path to file is correct.

But the download is never started.

Echoing the response gives:

HTTP/1.0 200 OK
0:                   Content-Type: application/csv
Cache-Control:       public
Content-Disposition: attachment; filename="Spots.csv"
Date:                Mon, 26 Oct 2015 16:08:26 GMT
Last-Modified:       Mon, 26 Oct 2015 16:08:26 GMT
1:                   Content-Description: File Transfer
2:                   Cache-Control: must-revalidate, post-check=0, pre-check=0



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire