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

Laravel 8上传PDF后无法显示(404错误)技术求助

问题分析与解决方法

1. 存储路径拼接错误

你的代码中存储路径的拼接逻辑存在问题:

$filePath = $pdf->storeAs('public/uploads', $fileName);
$fileModel->file = '/storage/' . $filePath;

storeAs('public/uploads', $fileName) 返回的内容是 public/uploads/xxx.pdf,拼接后生成的路径是 /storage/public/uploads/xxx.pdf,但Laravel通过storage链接访问文件的正确路径应该是 /storage/uploads/xxx.pdf。

修改代码为以下两种方式之一即可:

// 方式一:去掉路径中的public/前缀
$filePath = $pdf->storeAs('public/uploads', $fileName);
$fileModel->file = '/storage/' . str_replace('public/', '', $filePath);
// 方式二:直接拼接正确路径
$fileName = time().'.'.$pdf->getClientOriginalExtension();
$pdf->storeAs('public/uploads', $fileName);
$fileModel->file = '/storage/uploads/' . $fileName;

2. 未创建Storage符号链接

Laravel默认需要创建符号链接,让项目根目录下的public/storage指向storage/app/public目录,否则无法通过Web访问存储的文件。在终端执行以下命令:

php artisan storage:link

生产环境下需确保Web服务器拥有该链接的访问权限。

3. 验证视图中的路径使用

在pdfs.show视图文件中,确认使用的是修正后的$fileModel->file路径,示例代码:

<iframe src="{{ $fileModel->file }}" width="100%" height="600px"></iframe>

内容的提问来源于stack exchange,提问作者El Hussien Ramadan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 10:35:42