0

I have a problem with Excel export using(Maatwebsite). After downloading a file and when I try to open, it in Excel, It throws me "The file could not be opened [...] invalid format or extension".

I'm out of ideas, what I'm doing wrong?

Any Suggestions will be helpful.

web.php

Route::any('xyz', 'DocController@excel');

Query created by AJAX (POST with external data to query filter)

        [...]
        type: 'POST',
        url: '/xyz',
        data: window.option,
        dataType: 'json',
        cache: false,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        responseType: 'arraybuffer',
        success: function(response, textStatus, request) {
            var a = document.createElement('a');
            a.setAttribute("type", "hidden");
            a.setAttribute("target", "_blank");
            a.href = response.file;
            a.download = response.name;
            document.body.appendChild(a);
            a.click();
            a.remove();
        },
        [...]

End of controller function:

        [...]
        $file = Excel::download(new DocDocuments($data), 'data.xlsx');
        $response =  array(
            'name' => "data.xlsx", //no extention needed
            'file' => "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,".base64_encode($file) //mime type of used format
        );

        return response()->json($response);
Shibu
  • 486
  • 1
  • 11
  • 20
  • Does this answer your question? [Laravel Excel is working but the file cannot be opened](https://stackoverflow.com/questions/67609038/laravel-excel-is-working-but-the-file-cannot-be-opened) – OMi Shah Aug 30 '23 at 08:53
  • Unfortunately, the solution didn't work. I changed the download concepts completely (that solve my problem). – Noarifumirimota Aug 30 '23 at 11:37
  • Cool. You can share your solution as an answer to this question which might be helpful for future readers. :) – OMi Shah Aug 30 '23 at 11:43

0 Answers0