How to access pdf file from storage directly from browser?

三世轮回 提交于 2019-12-12 04:07:04

问题


I want to access a pdf file from the browser, the file is located in laravel storage folder. I don't want the storage to be public.

I don't want to download it(that I managed to do it). I simply want to have a get route, and to show that file in the browser like: www.test.com/admin/showPDF/123/123_321.pdf.

123 is an id.

If I use:

storage_path('app/'.$type.'/'.$fileName);
or
Storage::url('app/'.$type.'/'.$fileName);

return the full server path.

thanks.


回答1:


Add new route to get pdf

Route::get('/admin/showPDF/{$type}/{$fileName}','PDFController@pdf');

and in your controller

public function pdf($type,$fileName)
    {
        $path = storage_path('app/'.$type.'/'.$fileName);
        return response()->file($path);
    }



回答2:


You can read it from the storage folder and then stream the content to the browser and force the browser to download it.

$path = storage_path('app/'.$type.'/'.$fileName)

return Response::make(file_get_contents($path), 200, [
    'Content-Type' => 'application/pdf', //Change according to the your file type
    'Content-Disposition' => 'inline; filename="'.$filename.'"'
]);



回答3:


Quick and dirty, but what you want to do is use the path you grabbed in the response from a controller method (or route closure, your call). Something like:

public function sendPdf(Request $request)
{
    // do whatever you need to do here, then
    ...
    // send the file to the browser
    $path = storage_path('app/'.$type.'/'.$fileName);
    return response()->file($path);
}

See https://laravel.com/docs/5.4/responses#file-responses for more information on this, but that's how I'd go about it




回答4:


You have to stream your file in a request. In your controller do the following thing

use Symfony\Component\HttpFoundation\Response;

...

function showPdf(Request $request, $type, $fileName)
{
   $content = file_get_contents(storage_path('app/'.$type.'/'.$fileName));

   return Response($content, 200, [
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => "inline; filename=\"$fileName\""
        ]);
}

This will directly stream your PDF




回答5:


You can make an symbolink link between storage/app/public and public/storage so you can access your files, by running

php artisan storage:link

More info Here.

Then you can make a route like this to access the files:

Route::get('pdffolder/{filename}', function ($filename)
{
    $path = storage_path('app/public/pdffolder/' . $filename);

    if (!File::exists($path)) {
        abort(404);
    }

    $file = File::get($path);
    $type = File::mimeType($path);

    $response = Response::make($file, 200);
    $response->header("Content-Type", $type);

    return $response;
});

So in this case if you save a pdf named 123.pdf in the folder storage/app/public/pdffolder

you can access it by http://yourdomain.com/pdffolder/123.pdf

you have to tweak it a little bit, but i think this can help you.



来源:https://stackoverflow.com/questions/46002665/how-to-access-pdf-file-from-storage-directly-from-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!