baha2odeh/yii2-easy-fileupload-behavior
最新稳定版本:v1.0
Composer 安装命令:
composer require baha2odeh/yii2-easy-fileupload-behavior
包简介
Yii2 Easy File Upload Behavior
README 文档
README
Yii2 Easy File Upload Behavior
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist baha2odeh/yii2-easy-fileupload-behavior "*"
or add
"baha2odeh/yii2-easy-fileupload-behavior": "*"
to the require section of your composer.json file.
you have to not use UploadedFile::getInstance anymore this can be done by this behavior
Usage
Once the extension is installed, simply use it in your code by : add this on your php model
/** * {@inheritdoc} */ public function rules() { return [ [['images_files','image_file'],'file'], //@TODO add your rules here ]; } public function behaviors() { return [ [ 'class' => EasyFileUploadBehavior::className(), 'relations' => [ 'images_files' => 'images', // images means your HasMany Relation Name 'image_file' => 'image', // image means your HasOne Relation Name ], 'uploadCallBack' => function($relationName,UploadedFile $file){ // do your magic here and return one model that you save the image on it // if return is null file will skipped $file->saveAs('upload-path/'.$file->name); $image = new Image(); $image->filename = $file->name; $image->save(false); return $image; } ] ]; } //// demo only //// /** * @return \yii\db\ActiveQuery */ public function getImage() { return $this->hasOne(Image::className(), ['image_id' => 'image_id']); } /** * @return \yii\db\ActiveQuery */ public function getArticleImages() { return $this->hasMany(ArticleImage::className(), ['article_id' => 'id']); } /** * @return \yii\db\ActiveQuery */ public function getImages() { return $this->hasMany(Image::className(), ['image_id' => 'image_id'])->via('articleImages'); } //// demo only ////
then use it on your form like this
<?php $form = ActiveForm::begin(); ?> <div class="box-body table-responsive"> <?= $form->field($model, 'image_file')->fileInput() ?> <?= $form->field($model, 'images_files[]')->fileInput(['multiple'=>true]) ?> </div> <div class="box-footer"> <?= Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success btn-flat']) ?> </div> <?php ActiveForm::end(); ?>
统计信息
- 总下载量: 1.8k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2019-04-04