这是我的视图文件,它有一个表格应该发布文本变量数据为0000000005的文件。但由于某种原因,控制器内的加载功能不会填充这些数据。
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\models\customs\UploadForm */
/* @var $form ActiveForm */
?>
<div class="vendor_file_upload_form">
<?php $form = ActiveForm::begin(
['id' => 'form-files'],
['options' => ['enctype' => 'multipart/form-data']]
); ?>
<?= $form->field($model, 'AppFile')->fileInput() ?>
<?= $form->field($model,'apid')->textInput(); ?>
<div class="form-group">
<?= Html::submitButton(Yii::t('app', 'Submit'), ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div><!-- vendor_file_upload_form -->
这是我的控制器
public function actionFiles($id=null)
{
$model = new UploadForm();
if ($model->load(Yii::$app->request->post()))
{
$model->AppFile = UploadedFile::getInstance($model, 'AppFile');
if ($model->upload())
{
\Yii::$app->getSession()->setFlash('success', 'File has been successfully uploaded.');
return $this->redirect(['index']);
}
else
{
\Yii::$app->getSession()->setFlash('error', 'Wrong File Format , Try proper format.');
return $this->redirect(['index']);
}
}
}
我发现由于某种原因,加载数据不会加载所有已发布的表单参数。我花了3个小时,我仍然无法理解为什么我的表单数据没有正确传输。
我检查了Payload,我发现以下内容意味着我的数据正常,但加载功能无法正常加载。
------ WebKitFormBoundaryZfszVA0IZuj02ZFT Content-Disposition:form-data; NAME = “_ CSRF-后端”
UEZaLmZ4MzEWExBjLz8LSWAnHG9SGgFhaBcqbycRZWc1dGtCAD9JYg == ------ WebKitFormBoundaryZfszVA0IZuj02ZFT Content-Disposition:form-data; NAME = “UploadForm [AppFile]”
------ WebKitFormBoundaryZfszVA0IZuj02ZFT Content-Disposition:form-data; NAME = “UploadForm [AppFile]”;文件名=“猎鹰wallpaper.jpg” 内容类型:image / jpeg
------ WebKitFormBoundaryZfszVA0IZuj02ZFT Content-Disposition:form-data; NAME = “UploadForm [APID]”
0000000005 ------ WebKitFormBoundaryZfszVA0IZuj02ZFT -
编辑: 下面是我的模型文件,
<?php
/**
* Created by PhpStorm.
* Date: 1/8/2017
* Time: 9:52 PM
*/
namespace backend\models\customs;
use backend\models\ApplicationFiles;
use yii\base\Model;
use yii\web\UploadedFile;
/**
* UploadForm is the model behind the upload form.
*/
class UploadForm extends Model
{
/**
* @var UploadedFile
*/
public $AppFile; //files which needs to be uploaded
public $apid; //application_id
/**
* @return array the validation rules.
*/
public function rules()
{
return [
[['apid'], 'required','message'=>'No Application Id found'],
[['AppFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
];
}
public function upload()
{
//upload on the backed/uploads folder with time stamp as the file name
if ($this->validate()) {
$File_name = time() . '.' . $this->AppFile->extension;
if($this->AppFile->saveAs(\Yii::$app->basePath.'/uploads/' .$File_name ))
{
$model = new ApplicationFiles();
$model->name = $File_name;
$model->path = \Yii::$app->basePath.'/uploads/' .$File_name;
$model->application_id = $this->apid;
// $model->type =
$model->save();
}
return true;
}
else
{
return false;
}
}
}
?>