Yii2视频上传显示错误#400

时间:2016-10-15 12:54:49

标签: file-upload yii2

请上传图片,但上传视频会显示<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MyActivity"> <EditText android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textAppearance="?android:attr/textAppearanceMedium" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1"> <RatingBar android:id="@+id/rating" style="?android:attr/ratingBarStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:numStars="5" android:rating="4" android:stepSize="1" /> </LinearLayout> </LinearLayout> 。请问可能是什么原因?我没有在Controller中编辑Bad Request (#400) Unable to verify your data submission。感谢您的帮助。以下是代码(Yii2.0.8) 的模型即可。

enableCsrfValidation

查看

public function uploader() {

    $file = UploadedFile::getInstanceByName('file');
    if(!$this->validate()){
            \Yii::$app->session->setFlash('warning', implode(', ', $this->firstErrors));
            return FALSE;
        }

    if($file && $file !== NULL){
        $pic = $this->mediaType($file);
        $path = Yii::getAlias('@webroot').$pic;
        if(!$file->saveAs($path)){
            Yii::$app->session->setFlash('error', 'Upload Error: '.$file->error);
            return FALSE;
        }
        $this->file = Yii::getAlias('@web').$pic;
    } elseif (!$this->isNewRecord) {
        //no upload was initiated and it is not a new record, assign old value
        $this->file = $this->getOldAttribute('file');
    } else {
        //it is a new record and no upload was initiated
        $this->file = '';
    }

    if(!$this->save('false')){
        Yii::$app->session->setFlash('error', 'Saving To Database Error: '. implode(', ', $this->firstErrors));
        return FALSE;
    }
    Yii::$app->session->setFlash('success', 'SAVED SUCCESSFULLY!');
    return TRUE;
}

/**
 * check if extention is in video format to determine which folder to save file
 * @param UploadedFile $file
 * @return string
 */
public function mediaType(UploadedFile $file) {
    if(\yii2mod\helpers\ArrayHelper::isIn($file->extension, ['mp4', '3gp', 'mpeg4'])){
        return '/files/videos/users/posts/'.Yii::$app->user->id.'-'.$file->baseName.'.'.$file->extension;
    }
    return '/files/images/users/posts/'.Yii::$app->user->id.'-'.$file->baseName.'.'.$file->extension;
}  

1 个答案:

答案 0 :(得分:1)

尝试将enableCsrfValidation设置为false,默认为true且您尚未发送_csrf,这会产生错误。

PS:对不起我的英语)