这是我的模型,我已按照我所遵循的教程添加了所需的所有内容。
此代码用于上传所选文件,但不适用于拖动文件。
<?php
namespace backend\models;
use Yii;
/**
* This is the model class for table "documents".
*
* @property integer $id
* @property string $ref_id
* @property string $heading
* @property integer $doc_type
* @property string $doc_remarks
* @property string $doc_path
* @property string $created
* @property string $updated
* @property string $deleted
*/
class Documents extends \yii\db\ActiveRecord
{
public $file;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'documents';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['doc_path'], 'required'],
[['ref_id','doc_type'], 'integer'],
[['deleted', 'heading', 'doc_type'], 'string'],
[['file'],'safe'],
[['file'],'file'],
[['ref_id'], 'string', 'max' => 100],
[['heading', 'doc_remarks'], 'string', 'max' => 1000],
[['doc_path'], 'string', 'max' => 500],
[['created', 'updated'], 'string', 'max' => 50],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'ref_id' => Yii::t('app', 'Ref ID'),
'heading' => Yii::t('app', 'Heading'),
'doc_type' => Yii::t('app', 'Doc Type'),
'doc_remarks' => Yii::t('app', 'Doc Remarks'),
'doc_path' => Yii::t('app', 'Doc Path'),
'created' => Yii::t('app', 'Created'),
'updated' => Yii::t('app', 'Updated'),
'deleted' => Yii::t('app', 'Deleted'),
];
}
}
这是我的观点,
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\models\Documents */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="documents-form">
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="panel-title">Compare Quotation</h2>
</div>
<div class="panel-body">
<?= $form->field($model, 'ref_id')->textInput() ?>
<?= $form->field($model, 'file')->fileInput(['class' => 'file', 'data- preview-file-type' => 'any', 'data-upload-url' => '#']) ?>
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Save') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success pull-right' : 'btn btn-primary pull-right']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
这是我的控制者,
use yii\web\UploadedFile;
public function actionCreate()
{
$model = new Documents();
//file upload
$path = Yii::getAlias('@backend').'/web/';
if ($model->load(Yii::$app->request->post())) {
//variable which declare in model---file
$model->file = UploadedFile::getInstance($model, 'file');
$save_file = '';
if($model->file){
$imagepath = 'docs/rec_quotations/'; // Create folder under web/images/logo
//save into database
$model->heading = $model->file;
$model->doc_path = $imagepath.$model->file;
$save_file = 1;
}
if ($model->save(false)) {
if($save_file){
$model->file->saveAs($path.$model->file);
}
}
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
请帮帮我!!提前谢谢。