Laravel - Accessing absolute directory path for download

Asked

Viewed 2,536 times

0

I created a directory within /public called /uploads/pdf. In this directory I keep PDF files from which I need to download. I created an AJAX request for this that calls an application controller for this, however, via AJAX does not work.

If I make the call it usually works.

Look at the code:

Pdfclass.php //Class I created globally registered

public static function createPDF($view){
    define('BUDGETS_DIR', public_path('uploads/pdf'));
    if (!is_dir(BUDGETS_DIR)){
        mkdir(BUDGETS_DIR, 0755, true);
    }

    $outputName = str_random(10);
    $pdfPath    = BUDGETS_DIR.'/'.$outputName.'.pdf';
    File::put($pdfPath, PDF::load($view, 'A4', 'portrait')->output());
}

As you can see, he already has the call to download the file.

In the controller I call this method to create the report and return.

Reservacontroller.php

   PDFClass::createPDF(HTMLRelatorio::confirmacaoReserva($fileServico,$motorista));

That creates the PDF file, saves it in the specified directory, but... Does not open the download window because the call is by AJAX...

The call in the view: reserved.blade.php

$(this).on('click','#imprimirConfirmacao',function(){
    var idFile = $('#id_file').val();
    var idServ = $('#id_servico').val();
    $.ajax({
        url: '/reserva/imprimir',
        type: 'POST',
        dataType: 'html',
        data: {idFile: idFile,idServ:idServ},
        success: function(data){
            console.log(data);
        }
});

You could help me with that. If someone can give me a way to download the file via ajax or a way to access the absolute path to create a link by Laravel I appreciate, because if I make the call through the direct browser also returns error:

Look at:

http://meusite.com/uploads/pdf/arquivo.pdf OR http://meusite.com/public/uploads/pdf/arquivo.pdf

Returns error:

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
  • This request is created because the report is created on time...

2 answers

1

Do you have a route called 'uploads'? If yes, this error may be happening because it does not access the file in the uploads folder, but tries to find the action of this route... There are no impediments to accessing the files in the /public folder, except if there is a route with the same name as one of the files or folders.

  • This does not provide an answer to the question. To criticize or request clarification from an author, leave a comment below its publication.

0

When that happens, I sort it out. I tag data-url-base or base date, however you prefer, in the body. See below:

Base path of your site. Type http://localhost/your system/public/ <body data-url-base="{{ URL::to('') }}">

So in your JS do so:

// Variável Global - Tem que vir primeiro de tudo.
var urlBase; 

// Dentro do document.ready

$(document).ready(function(){
urlBase = $('body').data('url-base');  
)}

Then in your Ajax on the line: url: '/reserva/imprimir',

Put the variable urlBase

url: urlBase + '/reserva/imprimir',

And in addition your function should have the same link name in the Ajax URL. You are calling via POST the URL /reserve/print. Therefore, your function that will download the PDF should be :

public function postImprimir()

That’s how you’re doing it ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.