Laravel - Accessing directory absolute path for download

0

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

If I make the call, it usually works.

See 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.

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

Could you help me with this. If someone can pass me a way to download the file via ajax or a way to access the absolute path to create a link by Laravel I thank you, because if I make the call by the direct browser also returns error:

See:

link OR link

Returns error:

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
    
asked by anonymous 07.06.2014 / 19:16

2 answers

1

Do you have any route called 'uploads'? If yes, this error may be happening so it does not access the file in the uploads folder, but try to find the action of that route ... There are no restrictions on accessing the files in the / public folder, unless there is a route with the same name as one of the files or folders.

    
22.06.2014 / 00:40
0

When that happens, I'll sort it out. I put a date-url-base or base-date tag, as you like, in body . See below:

The base path of your site. Type link <body data-url-base="{{ URL::to('') }}">

From there on 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 on your Ajax line: url: '/reserva/imprimir',

Put the variable urlBase

url: urlBase + '/reserva/imprimir',

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

public function postImprimir()

Is that what you're doing?

    
17.04.2015 / 13:55