简体   繁体   English

Laravel 5.2从存储目录访问文件

[英]Laravel 5.2 Access a file from storage directory

I have placed my uploads in the storage folder. 我已将上传内容放入存储文件夹中。 Now, I want to display these uploads into my view. 现在,我想在我的视图中显示这些上传内容。 I have tried doing this: 我试过这样做:

view: 视图:

<img src="{{route("image_download", ['id' => Auth::guard('admin')->user()->AccountOfficerID, 'filename' => Session::get('avatar')] )}}" class=img-circle alt="Avatar" />

route: 路线:

Route::get('images/{id}/{$filename}', ['as' => 'image_download', function ($id, $filename)
{
    $path = storage_path() . '/uploads/images/admins/' . $id . '/icon_size/' . $filename;

    if (File::exists($path)) {
        $type = File::mimeType($path);

        return response()->download($path);
    }
}]);

And other various methods I have searched. 以及我搜索过的其他各种方法。 But none worked for me. 但没有一个对我有用。 I have checked the path and it is correct. 我检查了路径,这是正确的。 However, it is not displaying any image at all. 但是,它根本不显示任何图像。 I hope someone could point me to the right track. 我希望有人能指出我走上正轨。 Thank you. 谢谢。

EDIT There seems to be a 404 error in the browser console relating to the file. 编辑浏览器控制台中似乎有404错误与文件有关。 But I can access the file with the path returned through the browser. 但我可以使用浏览器返回的路径访问该文件。

It was just an error in the parameters. 这只是参数中的一个错误。 I have accidentally placed a $ sign. 我不小心放了一个$符号。 Thank you for all the help. 谢谢你的帮助。

First of all have you modified your config/filesystems.php? 首先你修改了你的config / filesystems.php吗?

Also you don't do as you dont need to call route() in src image. 你也不需要在src图像中调用route()。

`<img src="{{route("image_download", ['id' => Auth::guard('admin')->user()->AccountOfficerID, 'filename' => Session::get('avatar')] )}}" class=img-circle alt="Avatar" />`

Instead do asset() and put the route in your <a> if you want it linked 而是执行asset()并将路径放在<a>如果您想要链接的话

<img src="{{asset("image_source")}}" class=img-circle alt="Avatar" />

Have a look at this tutorial on how to use Laravel Storage correctly. 查看本教程,了解如何正确使用Laravel Storage

试试这个:

<img src="{{ url('image_download/'.$id.'/'.$filename) }}"/>

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

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