如何在Laravel 6中返回图像?

如何在Laravel 6中返回图像?,laravel,laravel-6,laravel-storage,laravel-filesystem,Laravel,Laravel 6,Laravel Storage,Laravel Filesystem,我有以下代码: 公共函数showImage($id){ $item=File::find($id); return response()->file($item->file\u name\u和\u path); } $item->file\u name\u和\u path包含以下内容: files/2020/04/29/myfile.jpeg 现在我总是得到一个FileNotFoundException 映像文件位于本地驱动程序上,路径为: {laravel root}/storage/ap

我有以下代码:

公共函数showImage($id){
$item=File::find($id);
return response()->file($item->file\u name\u和\u path);
}
$item->file\u name\u和\u path
包含以下内容:

files/2020/04/29/myfile.jpeg
现在我总是得到一个
FileNotFoundException

映像文件位于本地驱动程序上,路径为:

{laravel root}/storage/app/files/2020/04/29/myfile.jpeg

如何获取正确的本地路径,或者只返回带有图像HTTP头的图像文件?

您可以使用存储外观来实现这一点:

use Illuminate\Support\Facades\Storage;

public function showImage($id) {
    $item = File::find($id);

    return Storage::response($item->file_name_and_path);
}
如您所见,它将向响应中添加以下标题:

'Content-Type'
'Content-Length'
'Content-Disposition'

使用
returnresponse()->with('Image\u path',$item->file\u name\u和\u path)您需要文件路径还是文件url?