在Laravel 5中显示本地磁盘上的pdf文件? [英] Display pdf file from local disk in Laravel 5?

查看:97
本文介绍了在Laravel 5中显示本地磁盘上的pdf文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Laravel 5.5应用程序,具有管理员权限的用户可以在其中上传文件.他们上传文件后,我希望他们能够在管理员仪表板中查看文件.

I have a Laravel 5.5 app where users with administrator privileges can upload files. After they upload the files I'd like them to be able to view the file in the administrator dashboard.

我有一个 DocumentController.php ,用于处理文件上传到本地磁盘上的情况:

I have a DocumentController.php that handles the file upload to the local disk:

public function store(Request $request)
{
    // check to make sure user is an admin
    $request->user()->authorizeRoles('admin');

    // validate that the document is a pdf and 
    // that required fields are filled out
    $this->validate($request, [
        'title' => 'required',
        'description' => 'required',
        'user_id' => 'required|exists:users,id', 
        'document_path' => 'required|mimes:pdf'
    ]);

    $file = $request->file('document_path');

    $path = $file->store('documents/' . $request->user_id);

    $document = Document::create([
        'user_id' => $request->user_id,
        'title' => $request->title,
        'description' => $request->description,
        'file_path' => $path
    ]);

    return redirect($document->path());
} 

此方法从表单中获取文件,确保它是pdf,然后将文件保存到 storage/app/documents/{user_id} .然后,它将在数据库中创建一个文档记录,并根据文档ID将其转发到URL:/admin/document/{$ document-> id}

This method takes the file from the form, makes sure it is a pdf and then saves the file to storage/app/documents/{user_id}. It then creates a Document record in the database and forwards to the URL based on the document id: /admin/document/{ $document->id }

该路线定义为Route::get('/admin/document/{document}', 'DocumentController@show');

我在控制器中的哪个位置将文档传递给视图:

Where in the controller I pass the document to the view:

public function show(Document $document, Request $request)
{
    // check to make sure user is an admin
    $request->user()->authorizeRoles('admin');

    $storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();

    return view('admin.document', compact('document', 'storagePath'));
}

我要在该页面上显示pdf文档.

On that page I would like to display the pdf document.

资源/视图/admin/document.blade.php

@extends('layouts.app')

@section('content')
<div class='container'>
    <div class='row'>
        <div class='col-sm-2'>
            <a href='/admin'>< Back to admin</a>
        </div>
        <div class='col-sm-8'>
            {{ $document }}

            <embed src="{{ Storage::url($document->file_path) }}" style="width:600px; height:800px;" frameborder="0">

        </div>
    </div>
</div>
@endsection

我尝试使用$storagePath变量和Storage方法,但是无法在iframe中显示pdf文件.

I have tried using the $storagePath variable and Storage methods but cannot get the pdf file to display within the iframe.

使用本地文件存储,如何在浏览器中显示文件?另外,我保护了路由,以便只有管理员才能查看文档页面,但是保护文档本身路径的最佳方法是什么?

Using local file storage how would I display the file in the browser? Also, I've protected the route so that only admins can view the document's page but what is the best way to secure the path to the document itself?

推荐答案

如果要保护文件(只有管理员可以访问它们),则需要创建新的路由和新的DocumentController方法getDocument

If you want your files to be protected (only admin can access them), then you need to create a new route and new DocumentController method getDocument

添加新路线

Route::get('documents/pdf-document/{id}', 'DocumentController@getDocument');

DocumentController 中,添加

use Storage;
use Response;

添加新的方法,该方法将从存储中读取您的pdf文件并将其返回

Add new method that will read your pdf file from the storage and return it back

public function getDocument($id)
{
    $document = Document::findOrFail($id);

    $filePath = $document->file_path;

    // file not found
    if( ! Storage::exists($filePath) ) {
      abort(404);
    }

    $pdfContent = Storage::get($filePath);

    // for pdf, it will be 'application/pdf'
    $type       = Storage::mimeType($filePath);
    $fileName   = Storage::name($filePath);

    return Response::make($pdfContent, 200, [
      'Content-Type'        => $type,
      'Content-Disposition' => 'inline; filename="'.$fileName.'"'
    ]);
}

在您看来,您可以显示这样的文档

In your view you can show the document like this

<embed
    src="{{ action('DocumentController@getDocument', ['id'=> $document->id]) }}"
    style="width:600px; height:800px;"
    frameborder="0"
>

这篇关于在Laravel 5中显示本地磁盘上的pdf文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆