dsideal2015 2016-06-29 18:02
浏览 72

Yii2。 上传文件时出错

My Code related is the following:

Model Rules

[['documentTypeId', 'itemId', 'name', 'document'], 'required'],
            [['document'], 'file', 'skipOnEmpty' => false, 'extensions' => ['png', 'jpg', 'doc', 'pdf'], 'checkExtensionByMimeType'=>false],

Model method

public function upload($file)
    {
        if ($this->validate()) {
            $userFolder = Yii::getAlias("@app")."/uploads/".$this->item->userId;
            if(BaseFileHelper::createDirectory($userFolder) !== false) {
                $fileName = uniqid(rand(), false) . '.' . $this->document->extension;
                $file->saveAs($userFolder.'/' . $fileName);

                $this->document = $file->name;

                return true;
            } else {
                return false;
            }

        } else {
            return false;
        }
    }

Controller

$model = new ItemDocument();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $file = UploadedFile::getInstance($model, 'document');

            if($model->upload($file) !== false) {
                $model->save();
            }

            return $this->redirect(['view', 'id' => $model->id]);
        }



        return $this->render('create', [
            'model' => $model,
        ]);

This is giving me a validation error: "Document can not be blank". If I set "Document" field as not required and submit form I get "Please upload a file."

I am uploading this through a form with some other fields.

Any ideas?

  • 写回答

1条回答 默认 最新

  • duanfen9090 2016-06-29 18:07
    关注

    I have found the error it was the model->validate() on controller. When i do:

    $model->load(Yii::$app->request->post()) 
    

    File content is not loaded to the "document" field. Yii generates a hidden field which is empty. So I need first to do this:

    $file = UploadedFile::getInstance($model, 'document');
    

    So now my controller looks like this:

    $model = new ItemDocument();
    
            if ($model->load(Yii::$app->request->post())) {
    
                $model->document = UploadedFile::getInstance($model, 'document');
    
                if($model->validate()) {
    
                    if ($model->upload() !== false) {
                        $model->save();
                    }
    
                    return $this->redirect(['view', 'id' => $model->id]);
                }
            }
    
            return $this->render('create', [
                'model' => $model,
            ]);
    

    And I removed the validation inside upload method on model. Hope it helps someone.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!