【问题标题】:How to return image in Laravel 6?如何在 Laravel 6 中返回图像?
【发布时间】:2020-04-29 09:43:18
【问题描述】:

我有这个代码:

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

    return response()->file($item->file_name_and_path);
}

$item->file_name_and_path 包含以下内容:

files/2020/04/29/myfile.jpeg

现在我总是收到FileNotFoundException

图片文件在本地驱动的这个路径中:

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

我怎样才能获得正确的本地路径或简单地返回带有图像 HTTP 标头的图像文件?

【问题讨论】:

  • 使用return response()->with('Image_path',$item->file_name_and_path);
  • 你需要文件路径还是文件url?

标签: laravel laravel-6 laravel-storage laravel-filesystem


【解决方案1】:

您可以为此使用 Storage 外观:

use Illuminate\Support\Facades\Storage;

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

    return Storage::response($item->file_name_and_path);
}

如您所见here,它将在响应中添加以下标头:

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

【讨论】:

    猜你喜欢
    • 2020-01-22
    • 2015-03-19
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 2020-12-04
    • 1970-01-01
    相关资源
    最近更新 更多