2015-07-29 17:45:53 6527次浏览 2条回答 0 悬赏 20 金钱

Yii上传图片,当修改时,fileInput 没有提交图片,数据库中的图片路径被覆盖为空,请问如何解决?我想实现的是当修改时,如果没有重新上传图片,则图片地址还是保留之前的路径地址,求解决。谢谢。

最佳答案

  • 性感的农民 发布于 2015-07-29 18:40 举报

    有两个方法

    1.保存$model->pic旧的值,有重新上传就用新的,否则用旧的值
    2.没有重新上传,用unset($model->pic)将model中的pic属性去掉(这个我没有亲测过,网友和我说yii框架就是这么处理的)。

    6 条回复
    回复于 2015-08-03 17:20 回复

    这两个方法都不行啊,因为我在model页中有做了上传验证,array('pro_img','file','allowEmpty'=>true,'types'=>'jpg,jpeg,png,gif,bmp','maxSize'=>1024102410,'tooLarge'=>'图片上传的类型错误,或者上传文件大小大于10M')。如果没有进行上传,数据表中的'pro_img'就被自动设为空了。如果去掉file验证就可以的。这个真让人头疼。

    回复于 2015-08-04 09:48 回复

    应该不是这个原因吧,我也一样要在model中做验证,可以不用重新上传文件而保留表的字段值的。你是否可以贴出代码片段大家一起找一下原因?

    回复于 2015-08-04 11:14 回复

    这是控制器中的修改代码,请您看一下啊。
    public function actionEdit(){

        $model = Proinfo::model();
        $id = Yii::app()->request->getParam('id');
        $arrinfo = $model->findByPk($id,array(
            'select'=>array(
                'id',
                'fid',
                'pro_name',
                'pro_size',
                'pro_price',
                'pro_img',
                'content',
                'content2',
                'content3',
                'istj',
            ),
        ));
        //得到下拉列表数组(array('28'=>'产品分类1'))
        $arrsel = array();
        $arrsel[] = '请选择产品分类';
        foreach($this->getselect() as $v){
            $arrsel[$v->id] = $v->cls_name;
        }
    
        //在视图中显示读取出来的数据
        $model->fid = $arrinfo->fid;
        $model->pro_name = $arrinfo->pro_name;
        $model->pro_price = $arrinfo->pro_price;
        $model->istj = $arrinfo->istj;
        $model->pro_size = explode(',',$arrinfo->pro_size);
        $model->pro_img = $arrinfo->pro_img;
        $model->content = $arrinfo->content;
        $model->content2 = $arrinfo->content2;
        $model->content3 = $arrinfo->content3;
    
        if(isset($_POST['Proinfo'])){
            $model->attributes = $_POST['Proinfo'];
    
            if(is_array($model->pro_size)){
                $prosize = rtrim(implode(',',$model->pro_size),',');
                $model->pro_size = $prosize;
            }
    
            //图片上传
            $file = CUploadedFile::getInstance($model,'pro_img');
            if(is_object($file)){
                $pre_rand = 'img_'.time().mt_rand(0,9999);
                $img_name = $pre_rand.'.'.$file->extensionName;
                $file->saveAs(UPLOAD_PATH.$img_name);
                $model->pro_img = $img_name;
            }else{
                //$model->pro_img = '1111111111';
                //unset($model->pro_img);
                $model->pro_img = $arrinfo->pro_img;;
            }
    
            $model->times = time();
            
            if($model->validate()){
                if($model->updateByPk($id,$model->attributes))
                    Yii::app()->user->setFlash('success','1');
            }
    
        }
        $this->render('edit',array('model'=>$model,'arrinfo'=>$arrinfo,'arrsel'=>$arrsel));
    }
    
    回复于 2015-08-04 11:37 回复

    感觉你的$model有点走弯路了,arrinfo才是你的model对象。按源码的解释,你的$model变量叫做AR静态对象。不过你这样用问题也不大。你出现的问题的关键点是,这一句话:

    $model->pro_img = $arrinfo->pro_img;;
    

    你注释掉试试。

    给你参考一下:

    $model = $this->loadModel($id);
    
    if (isset($_POST['FwVersion'])) {
        //保存POST之前的数据
        $oldImg = $model->strImgLocation;
        $oldBasicpkg = $model->strBasicpkgLocation;
        $oldFullOta = $model->strFullOtaLocation;
    
        //取POST的数据
        $model->attributes = $_POST['FwVersion'];
    
        $model->strImgLocation = CUploadedFile::getInstance($model, 'strImgLocation');
        $model->strBasicpkgLocation = CUploadedFile::getInstance($model, 'strBasicpkgLocation');
        $model->strFullOtaLocation = CUploadedFile::getInstance($model, 'strFullOtaLocation');
        if ($model->strImgLocation) {
            //根据时间戳重命名文件名,extensionName是获取文件的扩展名
            //$newimg = 'ota_'.time().'_'.rand(1, 9999).'.'.$model->strImgLocation->extensionName;
            $newimg = 'img_' . time() . '_' . rand(1, 9999) . '_' . $model->strImgLocation;
            $model->strImgLocation->saveAs('assets/uploads/img/' . $newimg);
            //将image属性重新命名
            $model->strImgLocation = 'assets/uploads/img/' . $newimg;
        } else {
            //不再上传文件时,POST得到的这个字段值是空的,所以需要使用旧的数据来更新表
            $model->strImgLocation = $oldImg; //$_POST['ImgLocation2'];
        }
    ...
    }
    
    回复于 2015-08-04 13:22 回复

    问题解决了,谢谢,是$model的原因。

    回复于 2020-04-08 23:15 回复

    请问下你这个问题是怎么解决的,我也遇到了!

    觉得很赞
  • 回答于 2019-03-27 16:08 举报

    'pluginEvents' => [

                //选择文件后给input加id
                'filebatchselected'=>"function(event, files) {
                    $('.field-shop-shop_pic').show().find('input').attr('id','shop_pic');
                }",
                'fileuploaded' => "function (object,data,previewId, index){console.log(data);
                    $('#shop_pic').val($('#shop_pic').val()+';'+data.response);
                }",
                'error' => "function (){
                    alert('图片上传失败');
                }",
                "filebeforedelete" => "function (event, key, jqXHR, data) {
                    console.log(key);
                    var aborted = !window.confirm('你确定要删除该图片吗?');
                    if (aborted) {
                        //window.alert('File deletion was aborted! ');
                    };
                    return aborted;
                }",
            ]
    
您需要登录后才可以回答。登录 | 立即注册
zhubangrui
试用期

zhubangrui 上海

注册时间:2012-02-28
最后登录:2015-08-04
在线时长:0小时44分
  • 粉丝2
  • 金钱25
  • 威望0
  • 积分25

热门问题