【问题标题】:How to access pdf file from storage directly from browser?如何直接从浏览器访问存储中的pdf文件?
【发布时间】:2017-09-02 07:29:17
【问题描述】:

我想从浏览器访问一个 pdf 文件,该文件位于 laravel 存储文件夹中。我不想公开存储。

我不想下载它(我设法做到了)。我只是想要一条获取路线,并在浏览器中显示该文件,例如:www.test.com/admin/showPDF/123/123_321.pdf。

123 是一个 ID。

如果我使用:

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

返回完整的服务器路径。

谢谢。

【问题讨论】:

    标签: laravel laravel-5.2


    【解决方案1】:

    添加获取pdf的新路径

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

    在你的控制器中

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

    【讨论】:

      【解决方案2】:

      您可以从存储文件夹中读取它,然后将内容流式传输到浏览器并强制浏览器下载它。

      $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】:

        快速而肮脏,但您想要做的是使用您在控制器方法(或路由关闭,您的调用)的响应中获取的路径。比如:

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

        请参阅https://laravel.com/docs/5.4/responses#file-responses 了解更多信息,但我会这样做

        【讨论】:

          【解决方案4】:

          您必须在请求中流式传输您的文件。在您的控制器中执行以下操作

          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\""
                  ]);
          }
          

          这将直接流式传输您的 PDF

          【讨论】:

            【解决方案5】:

            您可以在 storage/app/public 和 public/storage 之间建立符号链接,以便您可以通过运行访问您的文件

            php artisan storage:link
            

            更多信息Here

            然后您可以创建这样的路径来访问文件:

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

            因此,在这种情况下,如果您将名为 123.pdf 的 pdf 保存在文件夹 storage/app/public/pdffolder

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

            你必须稍微调整一下,但我认为这可以帮助你。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-01-04
              • 2021-08-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-03-18
              • 1970-01-01
              相关资源
              最近更新 更多