Yii2 不允许加载本地资源(图片) [英] Yii2 Not allowed to load local resource (Image)

查看:28
本文介绍了Yii2 不允许加载本地资源(图片)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制台下的检查元素中收到此错误

不允许加载本地资源:file:///C:/xampp/htdocs/Yii2System/backend/web/data/cust_images/user_1.jpg

在视图文件中:

 

<?= Html::img($info->getUserPhoto($info->user_photo), ['alt'=>'No Image', 'class'=>'img-circle']);?>

在控制器中:

公共函数 actionUserPhoto($uid){$model = $this->findModel($uid);$info = UserInfo::findOne($model->user_info_id);$info->scenario = '照片上传';$old_photo = $info->user_photo;if($info->load(Yii::$app->request->post())){$info->attributes = $_POST['U​​serInfo'];$info->user_photo = UploadedFile::getInstance($info,'user_photo');if($info->validate('user_photo') && !empty($info->user_photo)){$ext= substr(strrchr($info->user_photo,'.'),1);$photo = $old_photo;$dir1 = Yii::getAlias('@backend').'/web/data/user_images/';if(file_exists($dir1.$photo) && $photo!='no-photo' && $photo!= NULL) {取消链接($dir1.$photo);}if($ext!=null) {$newfname = $info->user_name.'_'.$info->user_id.'.'.$ext;$returnResults = $info->user_photo->saveAs(Yii::getAlias('@backend').'/web/data/user_images/'.$info->user_photo = $newfname);如果($returnResults){$info->save(false);}}}返回 $this->redirect(['view', 'id' => $model->user_id]);}return $this->renderAjax('photo_form', ['model' => $model, 'info' => $info, ]);}

谁能帮我解决这个问题...

解决方案

浏览器不会从本地文件系统加载图像.您需要托管它并从网络服务器加载它.

你可以尝试用这条路径解决这个问题

 file://C:/xampp/htdocs/Yii2System/backend/web/data

否则.. 将图像放在外部(免费)可访问互联网的服务器上

i got this error in inspect element under console

Not allowed to load local resource: file:///C:/xampp/htdocs/Yii2System/backend/web/data/cust_images/user_1.jpg

in view file:

 <div class="col-md-12 text-center">
            <?= Html::img($info->getUserPhoto($info->user_photo), ['alt'=>'No Image', 'class'=>'img-circle']); ?>
 </div>

in controller:

public function actionUserPhoto($uid)
{
    $model = $this->findModel($uid);
    $info = UserInfo::findOne($model->user_info_id);
    $info->scenario = 'photo-upload';
    $old_photo = $info->user_photo;

    if($info->load(Yii::$app->request->post()))
    {
        $info->attributes = $_POST['UserInfo'];
        $info->user_photo = UploadedFile::getInstance($info,'user_photo');

        if($info->validate('user_photo') && !empty($info->user_photo))
        {
            $ext= substr(strrchr($info->user_photo,'.'),1);
            $photo = $old_photo;
            $dir1 = Yii::getAlias('@backend').'/web/data/user_images/';

            if(file_exists($dir1.$photo) && $photo!='no-photo' && $photo!= NULL) {
                unlink($dir1.$photo);
            }
            if($ext!=null) {
                $newfname = $info->user_name.'_'.$info->user_id.'.'.$ext;
                $returnResults = $info->user_photo->saveAs(Yii::getAlias('@backend').'/web/data/user_images/'.$info->user_photo = $newfname);
                if($returnResults) {
                    $info->save(false);
                }
            }
        }
        return $this->redirect(['view', 'id' => $model->user_id]);
    }
    return $this->renderAjax('photo_form', ['model' => $model, 'info' => $info, ]);
}

who can help me to fix this problem...

解决方案

the browser don't load images off a local filesystem. You need to have it hosted and load it off a web server.

You can try solve this with this path

    file://C:/xampp/htdocs/Yii2System/backend/web/data

Otherwise .. place the image on an external (free) internet accessible server

这篇关于Yii2 不允许加载本地资源(图片)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆