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);