I'm trying to download a PDF from Laravel to AngularJS, and using a bunch of different examples I've sort of gotten to a point where a PDF file downloads, but it is corrupted in some way. Can anyone see why this might be? I've added my test route and request:
// TEST ROUTE
Route::get('download', function () {
if(Storage::disk('local')->exists('download.pdf')) {
$filePath = storage_path('app/download.pdf');
$headers = ['Content-Type: application/pdf'];
return response()->download($filePath, $name = null, $headers);
}
return response()->json(['File does not exist.']);
});
// TEST REQUEST
$http({
url: "/api/v1/download",
method: "GET",
responseType: "blob"
}).then(function (response) {
var url = URL.createObjectURL(new Blob([response], {type: 'application/pdf'}));
var a = document.createElement('a');
a.href = url;
a.download = 'download.pdf';
a.target = '_blank';
a.click();
// TODO: perform clean up of createObjectURL
// TODO: add IE9 Blob shim
}).catch(function (reason) {
console.log('FAIL', reason);
});
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2hU1GeF
via IFTTT
Aucun commentaire:
Enregistrer un commentaire