简体   繁体   English

如何从公共文件夹中获取图像文件作为 Laravel 中的响应?

[英]How to get image file from public folder as a response in Laravel?

These are different ways I have tried in my controller to get image as response.这些是我在控制器中尝试获取图像作为响应的不同方式。

My images resides in public folder/img .我的图像驻留在public folder/img

switch ($document->ext) {

    case 'doc':
        $img = File::get(url('/img/doc.png'));
        break;

    case 'docx':
        $img = File::get(asset('/img/docx.jpg'));
        break;

    case 'pdf':
        $img = File::get(public_path('/img/pdf.png'));
        break;

    case 'ppt':
        $img = File::get(url(public_path('/img/ppt.png')));
        break;

    default:
}
return new Response($img, 200);

I guess it is a better way in case we change the public_path我想这是一个更好的方法,以防我们改变 public_path

        case 'doc':
        return response()->file(public_path('img/doc.png'));
        exit;

在文件夹名称之前添加 public

$img = File::get(url('public/img/doc.png'));

Use this to download a file from public folder with the option to setting a name使用它从公共文件夹下载文件,并提供设置名称的选项

public function downloadSample(){
        return Response::download(public_path("folder/my_file.xlsx"),
                     'your_file_name_when_downloaded.xslx');
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM