不允许使用 Laravel 加载本地资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37415159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:51:50  来源:igfitidea点击:

Not allowed to load local resource with Laravel

phplaravellaravel-5.2

提问by Joe W

I have put in an upload form in my Laravel application, the file uploads and saves it but when I try to display it I get joew:1 Not allowed to load local resource: file:///C:/xampp/htdocs/socialNet/public/uploads/joew.png

我在 Laravel 应用程序中放入了一个上传表单,文件上传并保存,但是当我尝试显示它时,我得到了 joew:1 Not allowed to load local resource: file:///C:/xampp/htdocs/socialNet/public/uploads/joew.png

This is the root I put in the filesystems.php file

这是我放在 filesystems.php 文件中的根

'public' => [
            'driver' => 'local',
            'root' => storage_path('public/uploads'),
            'visibility' => 'public',
        ],

So the photo is being saved inside a folder called public inside the storage folder.

所以照片被保存在存储文件夹内一个名为 public 的文件夹中。

This is the function retrieving the file

这是检索文件的函数

 public function getUserAvatar()
    {
         if ($this->username) {
          $userAvatar = User::where('username', $this->username)->value('avatar');
            return "{$userAvatar}";
        }
        if (!$user) {
            abort(404);
        }

    }

and this is the format that it is saved in the database.

这是它保存在数据库中的格式。

C:\xampp\htdocs\socialNet\public\uploads\joew.png

回答by stephangroen

Your browser is not allowed to directly load that file from your harddrive. In your view you are probably getting something like this:

您的浏览器不允许直接从您的硬盘加载该文件。在您看来,您可能会得到这样的信息:

<img src="C:\xampp\htdocs\socialNet\public\uploads\joew.png" />

<img src="C:\xampp\htdocs\socialNet\public\uploads\joew.png" />

You need a URL for the browser to access this image. In your view you need something like:

您需要一个 URL 供浏览器访问此图像。在您看来,您需要类似的东西:

<img src="{{ asset('uploads/joew.png') }}" />

<img src="{{ asset('uploads/joew.png') }}" />

The asset()function from Laravel creates a URL from the publicfolder and appends the param you give it. So this will create a URL like: http://localhost/public/uploads/joew.png

asset()Laravel的函数从public文件夹创建一个 URL并附加你给它的参数。所以这将创建一个 URL,如: http://localhost/public/uploads/joew.png

Edit: The folder you specified (storage_path) is probably not readable for the browser at all. Change the uploads to public_path('uploads')and it will be accessible.

编辑:您指定的文件夹 (storage_path) 可能根本无法被浏览器读取。将上传更改为public_path('uploads'),它将可以访问。