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');
}
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.