Saturday, April 1, 2017

Open pdf file on browser with Laravel 5.4

I store some pdf files on /storage/app/documents directory with the following disk rule:

    'documents' => [
        'driver' => 'local',
        'root' => storage_path('app/documents'),
    ],

Then I have this on api routes:

$api->get('documents/{filename}', 'App\\Api\\V1\\Controllers\\DocumentController@show');

And this on my controller:

public function show($filename)
{
    $path = storage_path() . '/app/documents/' . $filename;

    return Response::make(file_get_contents($path), 200, [
        'Content-Type' => 'application/pdf',
        'Content-Disposition' => 'inline; filename="'.$filename.'"'
    ]);
}

I tried also the following but same result:

public function show($filename)
{
    $path = storage_path() . '/app/documents/' . $filename;

    return response()->file($path);
}

But, when I call the link for a file, I have just this on the browser instead, without any extra info:

Cannot GET /api/documents/phpIEENXT.pdf



via Tasos

Advertisement