I have a system made in Laravel 5.7 where i need show pdf files stored in private folders, to do this i made a controller that output the pdf as a binary file, the code of controller is this:
$file = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix().$ruta;
$filename = 'soporte.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
ob_end_clean();
readfile($file);
I have a button in a table which fires the modal and hold info to display pdf:
<a href="#" class="btn btn-info ver-soporte" data-type="pdf" data-recurso=""><i class="far fa-file-pdf"></i> Ver</a>
In the client side i have the following code for create the object and show this in a modal:
$('body').on('click','.ver-soporte', function(e){
e.preventDefault();
let btn = e.currentTarget;
let tipo = $(btn).data('type');
let recurso = $(btn).data('recurso');
if(tipo === "pdf"){
$('#PDFdoc-viewer').remove();
$('#IMG-viewer').hide();
let objeto = $('<object>')
objeto.attr('id','PDFdoc-viewer');
objeto.addClass('PDFdoc');
objeto.attr('width', '100%');
objeto.attr('height', '1000px');
objeto.attr('type', 'application/pdf');
objeto.attr('data',recurso);
objeto.appendTo('#visor-body');
} else {
$('#PDFdoc-viewer').hide();
$('#IMG-viewer').attr('src',recurso).show();
}
$('#view-soporte').modal('show');
});
But when i clicked those buttons only works on 2n tries, if i click .ver-soporte button first time, the pdf loads, when i click a second time, fails (independently if is the same button or other button), the third time, loads pdf (independently if is the same button or other button) and so on, in the intermediate request i get an "error 500" and cancelled in the network tab of developer tools.
Please help!
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire