Reputation: 307
So I created a PHP Controller to handle exporting data which is posted by JS. The problem is I can see it creates something in the console but the file download never starts. I tried using ->store (laravel excel) and keeping it in an export folder but again when I try to use
return \Response::download($result);
it still won't start the download. The problem I'm having is just getting the download to start.
Angular Controller
$scope.exportMatrix = function () {
var postData = {list: $scope.list, matrix: $scope.matrix};
$http({
method: 'POST',
url: '/export',
dataType: 'obj',
data: postData,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (data) {
console.log(data);
}).error(function (data) {
console.log("failed");
});
}
Route
Route::post('/export', 'ExportController@export');
PHP Controller
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App;
use Excel;
use Response;
class ExportController extends Controller {
public function export()
{
$excel = App::make('excel');
Excel::create('Test', function($excel) {
$excel->setTitle('new awesome title');
$excel->sheet('Sheet', function($sheet) {
$sheet->fromArray(array(
array('data1', 'data2'),
array('data3', 'data4')
));
});
})->export('xlsx');
}
Upvotes: 3
Views: 11301
Reputation: 195
To download Laravel Excel and React, this worked for me. I hope it helps someone.
$fileName = 'reportes/reporte_programacion.xlsx';
Excel::store(new ReporteProgramacionExport($request->FechaInicio,$request->FechaFin,$request->Activo), $fileName);
$file = Storage::get($fileName);
if ($file) {
$fileLink = 'data:application/vnd.ms-excel;base64,' . base64_encode($file);
// @chmod(Storage::disk('local')->path($filename), 0755);
// @unlink(Storage::disk('local')->path($filename));
}
return response()->json([
'success' => true,
'message' => 'Payroll are successfully exported.',
'result' => [
'fileName' => 'reporte_programacion.xlsx',
'fileBase64' => $fileLink,
]
],200);
async function descargaReporte(filtro) {
const { FechaInicio, FechaFin, Activo } = filtro;
await descargar_reporte({ FechaInicio, FechaFin, Activo }).then(response => {
if (isNotEmpty(response.fileName)) {
let download = document.getElementById("iddescarga");
download.href = response.fileBase64;
download.download = response.fileName;
download.click();
// handleSuccessMessages( intl.formatMessage({ id: "MESSAGES.DOWNLOAD.SUCESS" }));
toastSuccess(intl.formatMessage({ id: "MESSAGES.DOWNLOAD.SUCESS" }));
}
}).catch(err => {
handleErrorMessages(intl.formatMessage({ id: "SEGURIDAD.USUARIOS.MESSAGES.INFO" }), err)
}).finally(() => {
setLoading(false);
});
}
Upvotes: 0
Reputation: 307
In the end I used FileSaver.js to download the blob it sent back so just load up FileSaver and use it saveAs(blob).
$http({
method: 'POST',
url: '/api/v1/download',
dataType: 'json',
data: {
data:data
},
responseType: 'arraybuffer',
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (data) {
var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
saveAs(blob, title + ".xlsx");
}).error(function (data) {
console.log("failed");
});
I made sure to use
responseType: arraybuffer
and saveAs type:
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Because reasons.
Upvotes: 5