webyjh 2012-01-01 15:52:20 4326次浏览 1条回复 0 0 0
public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('title,content,cat_name','required'),            
        array('content', 'safe'),
        array('url','file','maxSize'=>800*1024,'tooLarge'=>'文件太大超过800KB',),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('id, title, content, url, cat_name', 'safe', 'on'=>'search'),
    );
}

控制器代码

public function actionCreate()
{
    $model=new Downloads;

    // Uncomment the following line if AJAX validation is needed
    $this->performAjaxValidation($model);
    if(isset($_POST['Downloads']))
    {
        $model->attributes=$_POST['Downloads'];
        if ($model -> validate()) {//执行验证
        {
            $model->url=CUploadedFile :: getInstance($model, 'url');//准备上传文件
            $extName = strtolower($model ->url->extensionName); //取类型
            $newName = date('YmdHis') . '_' . rand(1, 9) . '.' . $extName; //按时间生成新文件名
            $path = yii :: app() -> basePath . '/../uploads/downs/' . $newName; //设定上传目录
            $model -> url -> saveAs($path); //保存文件
            $model -> url=$newName;//重新设好文件名
                                
            if($model->save())            
                $this->redirect(array('view','id'=>$model->id));
        }            
    }
}

    $this->render('create',array(
        'model'=>$model,
    ));
}

现在明明选择了文件,也可以正常提交表单,同时也上传了文件,就是提交前显示文件不能为空有点不爽

您需要登录后才可以回复。登录 | 立即注册