Laravel从数据库中的路径显示pdf

我已经阅读了很多关于此的问题和博客,但无法解决我的问题。所以我正在上传一个pdf,它的路径存储在这样的数据库中:


E:\laravel12\public\pdf/1577916921.pdf

我正在显示这样的图像:


<iframe src="{{$pdf->data}}"  height="500" width="500" style="margin-Top :200px;"></iframe>

我收到此错误:


Not allowed to load local resource: file:///E:/laravel12/public/pdf/1577916921.pdf

如何解决这个问题?


控制器:


if ($req->hasFile('fpdf')) {

    $mime = "aaa";

    $image = $req->file('fpdf');

    $name = time().'.'.$image->getClientOriginalExtension();

    $destinationPath = public_path('pdf');

    $image->move($destinationPath, $name);

    $content = $destinationPath."/".$name;

    $up = new Picture();


    $up->name = $name;

    $up->mime = $mime;

    $up->data = $content;


    $up->save();

}


心有法竹
浏览 163回答 2
2回答

杨魅力

不要在数据库中保存完整路径像这样在数据库中的公共路径后保存pdf/1577916921.pdf然后你可以像这样打印pdf<iframe&nbsp;src="{{&nbsp;asset($pdf->data)&nbsp;}}"&nbsp;&nbsp;height="500"&nbsp;width="500"&nbsp;style="margin-Top&nbsp;:200px;"></iframe>

撒科打诨

public_path 使用磁盘路径,而不是 URL 路径,所以我建议你只存储名称而不存储完整的物理磁盘位置,并且每当你想显示它时,你可以使用 url() 函数来生成 URL
打开App,查看更多内容
随时随地看视频慕课网APP