You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 11中如何从私有磁盘存储在iframe中显示PDF?

在Laravel 11中实现私有磁盘PDF文件的iframe展示

直接用Storage::disk('private')->path($filename)获取本地路径是行不通的——私有存储目录(storage/app/private)不在Web服务器根目录下,浏览器无法直接访问这个路径。正确的做法是通过Laravel路由和控制器中转文件内容,具体步骤如下:

1. 定义路由

在routes/web.php中添加处理私有PDF请求的路由:

use App\Http\Controllers\PrivateFileController;

Route::get('private-pdf/{filename}', [PrivateFileController::class, 'showPdf'])->name('private.pdf.show');

2. 编写控制器方法

创建PrivateFileController(或复用现有控制器),实现文件读取和响应逻辑:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Response;

class PrivateFileController extends Controller
{
    public function showPdf(string $filename)
    {
        // 校验文件是否存在
        if (!Storage::disk('private')->exists($filename)) {
            abort(404, '文件不存在');
        }

        // 可选:添加权限校验,比如验证当前用户是否有权限访问该文件
        // if (!auth()->user()->can('view-pdf', $filename)) {
        //     abort(403, '无访问权限');
        // }

        // 返回流式响应,避免大文件占用过多内存
        return Storage::disk('private')->response($filename, null, [
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => 'inline; filename="' . $filename . '"'
        ]);
    }
}

这里用Storage::response()自动处理流式输出,同时设置Content-Disposition: inline确保浏览器在iframe中展示文件而非触发下载。

3. 在视图中嵌入iframe

在Blade视图里,通过路由生成的URL嵌入iframe:

<iframe src="{{ route('private.pdf.show', ['filename' => 'your-file-name.pdf']) }}" 
        width="100%" 
        height="600px"
        frameborder="0">
</iframe>

可选优化

  • 给路由添加auth中间件,限制仅登录用户访问:
    Route::get('private-pdf/{filename}', [PrivateFileController::class, 'showPdf'])
         ->name('private.pdf.show')
         ->middleware('auth');
    
  • 若需支持文件名包含特殊字符,可在路由中添加where规则:
    Route::get('private-pdf/{filename}', [PrivateFileController::class, 'showPdf'])
         ->name('private.pdf.show')
         ->where('filename', '.*');
    

内容的提问来源于stack exchange,提问作者Maaz Tajammul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 21:03:10