如何渲染存储在Laravel 7.14的存储/应用程序/公共目录中的图像?



我卡住了。 如何显示存储在 Laravel 7.14 的/storage/app/public 目录中的图像? 我已经尝试了我在堆栈溢出中找到的那些,甚至是我通过谷歌搜索找到的那些。

我试过这个解决方案

//Controller function
public function renderPic($filename) {

$path = '/storage/app/public/uploads/'.$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;
}

网络路线

Route::get('user/renderPic/{filename}', 'User@renderPic')->name('user.renderPic');

边栏选项卡视图文件

<img id="displayPic" src="{{ route('user.renderPic',$user->filename) }}">

它没有显示

无需定义路由和控制器方法来呈现图像。

您只需在Laravel应用程序的根目录中打开一个终端并运行

php artisan storage:link

这将为存储/应用程序/公共目录创建一个指向公共/存储的符号链接

在刀片文件中,您只需按如下所示渲染图像:

<img id="displayPic" src="{{ asset('storage/your-image-name.ext') }}">

最新更新