AbdulBazith's avatar

How to return the excel download to ajax ??

Guys i have button (download excel) if i press the button it moves to the ajax call with values from the textbox and goes to the function 'downloadExcel_excess_milk' in my controller

everything is fine. working well. but if i press the button i have wrote an alert in the ajax success . it is not displayed and excel download is not done.

But in the inspect->network i can see the ajax call if i press that it downloading the excel.

actually when clicking the button itself the excel must download

That this after excel sheet is converted it is not getting downloaded.

need to return it from controller to ajax

How to do that

this is my javascript


$('#excel').on('click',function(){


$.ajax({
    type : 'get',
    url : '{{URL::to('downloadExcel_excess_milk')}}',

    data:{
            'search_time': $('#search_time').val(),
            'fromdate': $('#fromdate').val(),
            'todate': $('#todate').val()
    },
success:function(data)
    {       
        alert('success excel downloaded');
    }


this is my controller

public function downloadExcel_excess_milk()
        {

            $ex = Excess_milk::get();

            foreach($ex as $e)
        {

            $ex_array[]=array(

                ' Date'=>$e->date,
                ' Time'=>$e->time,
                ' No of litre'=>$e->no_of_litre,
                ' note'=>$e->note
            );
        }
                Excel::create('salesdet', function($excel) use ($ex_array) {
                    $excel->sheet('mySheet', function($sheet) use ($ex_array)
                    {

                     return   $sheet->fromArray($ex_array);

                    });
               })->download('xlsx');


        }


i think ajax must have a return. but in controller where can i write the return statement

i tried


 $output= Excel::create('salesdet', function($excel) use ($ex_array) {
                    $excel->sheet('mySheet', function($sheet) use ($ex_array)
                    {

                     return   $sheet->fromArray($ex_array);

                    });
               })->download('xlsx');

return output

refer this image: https://imgur.com/a/fQ83g5S

see the image netwrok tab. if i double click the blue portion then the excel is downloaded whats the problem??

Kindly some one suggest

Anyone reply please

0 likes
4 replies
Tomi's avatar

I would suggest to do a BLOB when downloading a file with ajax. In my case im using axios but that should not be any different to make with ajax.

        let laddaExportButton = Ladda.create(document.querySelector('.ladda-export-button'));
        laddaExportButton.start();
        laddaExportButton.setProgress(1);

        axios({
            url: '/download/path/to/excel',
            method: 'GET',
            responseType: 'blob',
            params: params
        }).then((response) => {
            const url = window.URL.createObjectURL(new Blob([response.data]));
            const link = document.createElement('a');
            link.href = url;
            link.setAttribute('download', page + '_' + moment(moment.now()).format("DD_MM_YYYY_HH_mm_ss") + '.xls');
            document.body.appendChild(link);
            link.click();
            laddaExportButton.stop();
        }).catch((error) => {
            laddaExportButton.stop();
        });

In my backend i have a lot of classes built around the download but in the end i just do this:

i just download the file like you do

resolve(Excel::class)->download($this, $fileName, $writerType ?? $this->writerType ?? null);
1 like
JenuelDev-31676163's avatar

@Tomi thanks for this one, I am trying to use $.ajax jquery because jquery is being used by some of our clients on their old projects. I think their is a problem with ajax. end up using axios.

AbdulBazith's avatar

@Tomi thankz for your reply

i tried this


resolve  (Excel::class)->create('salesdet', function($excel) use ($ex_array) {
                    $excel->sheet('mySheet', function($sheet) use ($ex_array)
                    {
 
                     $sheet->fromArray($ex_array);
 
                    });
               })->download('xlsx');

but error

Call to undefined method Maatwebsite\Excel\Facades\Excel::create()

My problem is just everything is fine

after ajax success i wrote a message alert("success").

But the function is coming inside it.

in controller there must be a return so only the success message displays

am i right??

i think u understood my problem.

the same thing i used for my pdf.

  $pdf=PDF::loadView('Excess_milk.viewpdf', [
            'exs' => $exs,
            'time'=>$request->search_time,
            'no_of_litre_tot' => $exs->sum('no_of_litre'),
            'fromdate'=>$request->search_fromdate,
            'todate'=>$request->search_todate
        ]);

       $pdf->setPaper('A4');

        return $pdf->download('excess.pdf');


this works fine press the button from view it moves to ajax comes to success and the pdf is downloaded problem is return in excel??

Do have some other solution or suggestion

please

1 like
AbdulBazith's avatar
AbdulBazith
OP
Best Answer
Level 5

according to @tykus answer this worked

The data will be transformed to a query string on an AJAX GET request; just do that yourself using the jQuery param function:

$('#excel').on('click',function(){
    var query = {
        location: $('#location').val(),
        area: $('#area').val(),
        booth: $('#booth').val()
    }


    var url = "{{URL::to('downloadExcel_location_details')}}?" + $.param(query)

   window.location = url;
});
2 likes

Please or to participate in this conversation.