vendredi 22 avril 2016

Laravel Excel - Ajax and JSON

Hey I am trying to generate a .xls file in my Single-page application. The data I have is an array of JSON objects. Now, the problem is that when I do an ajax call with the json data to my controller:

$.post('/api/exportJSON', {data:jsonData, exportOptions: eOptions}, function(res){
    console.log(res);
}); 

It returns:

��ࡱ��������������������

This is my route:

Route::group(array('prefix' => 'api'), function() {
    Route::any('/exportJSON', 'ApiController@exportJSON');
});

This is my controller method:

 public function exportJSON(){

    $filename = 'Test list';
    $filetype = 'xls';

    Excel::create($filename, function($excel) {

        $excel->setTitle('test')
              ->setCreator(Auth::user()->name)
              ->setDescription('test')
              ->setCompany('test')
              ->sheet('test', function($sheet) {

                    $sheet->setOrientation('landscape');

                });


        })->store($filetype)->export($filetype);
}

How should I do it? Btw it's not a must to do an ajax call, I can also do with a form, but I must be able to send parameter (the json list and options) to my export method. I also tried to make a post form like so in my view:

{!! Form::open(array('url' => 'api/exportJSON', 'method' => 'post', 'class' => 'form')) !!}

//insert hidden fields with the options here...

{!! Form::submit('Download xls', array('class'=>'btn btn--green')) !!}

{!! Form::close() !!}

But that doesn't work, i get a page not found error..

BUT when i browse to localhost/api/exportJSON it automatically downloads the .xls correctly and when i make a link <a href="/api/exportJSON">Download</a> in my view, when i click Download it downloads the .xls file but how can i send parameters? Can i add a huge list as GET params in that url?

Any ideas?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire