无法使用kartik小部件在yii2上传图像

时间:2016-10-14 10:12:01

标签: yii2

这是我的控制器我很困惑所以让我知道这里的错误是什么

 $imgName = $model->uploadimg;
            $model->file = UploadedFile::getInstance($model, 'uploadimg');
            $model->file->saveAS('uploads/'.$imgName.'.'.$model->file->extension);
            $model->uplaodimg='uploads/'.$imgName.'.'.$model->file->extension;

 This is view 
echo $form->field($model, 'uploadimg')->widget(FileInput::classname(), [
    'options' => ['accept' => 'image/*'],
]);

2 个答案:

答案 0 :(得分:1)

Activeform选项enctype设置为multipart/form-data,如下所示:

<?php $form = ActiveForm::begin([
    'options' => ['enctype'=>'multipart/form-data']
]); ?>

答案 1 :(得分:0)

查看:

use yii\web\UploadedFile;

<?= $form->field($model, 'uploadimg')->fileInput(['multiple' => true, 'accept' => 'image/']) ?>

控制器:

public function actionCreate()
{
        $model = new Model_Name();
        if ($model->load(Yii::$app->request->post())) {
            $model->uploadimg = UploadedFile::getInstance($model, 'uploadimg');
            $model->save();                
        }
}