yiicod / yii2-fileupload
基于jQuery-File-Upload的Yii框架文件上传组件
dev-master
2018-11-03 21:35 UTC
Requires
- blueimp/jquery-file-upload: 9.*
- symfony/filesystem: ~3.0|~4.0
- yiicod/yii2-base: 1.*
- yiisoft/yii2: 2.*
Requires (Dev)
This package is auto-updated.
Last update: 2024-08-27 00:53:59 UTC
README
基于blueimp jquery-file-upload的文件上传组件。您可以轻松为上传器编写主题。本扩展提供了在服务器上上传文件的全部工作流程。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require --prefer-dist yiicod/yii2-fileupload "*"
或
"yiicod/yii2-fileupload": "*"
将其添加到您的composer.json文件中的require部分。
配置
设置到控制器
public function actions() { return array( 'fileUpload' => [ 'class' => 'yiicod\fileupload\actions\FileUploadAction', ], ); }
为模型添加行为
public function behaviors() { return [ 'FileUploadBehavior' => [ 'class' => 'yiicod\fileupload\models\behaviors\FileUploadBehavior', 'sourceRepositoryClass' => [ 'class' => SourceRepository::class, 'uploadDir' => Yii::getAlias('@webroot/uploads'), // Base dir for file 'uploadUrl' => '/uploads', // Base url to folder ], 'fields' => array('logo'), ], ]; }
使用方法
FileUploadWidget::widget([ 'id' => 'fileuploader', 'model' => Model::class, 'attribute' => 'modelAttribute', 'allowedExtensions' => array('jpeg', 'jpg', 'gif', 'png'), 'maxFileSize' => 2 * 1024 * 1024, // limit in server-side and in client-side 2mb 'uploadDir' => Yii::getPathOfAlias('@webroot/uploads/temp'), // temp base dir 'uploadUrl' => Yii::$app->getBaseUrl(true) . '/uploads/temp/', // temp base url 'uploader' => UserAvatar::class, 'userData' => [], // Any data for UploaderInterface 'maxUploads' => -1, // defaults to -1 (unlimited) 'theme' => [ 'class' => BaseTheme::class, //Implements yiicod\fileupload\base\ThemeInterface 'multiple' => false, // allow to add multiple file uploads 'buttonText' => 'Upload file', 'dropFilesText' => 'Upload or Drop here', 'clientOptions' => array( //For chunk uploded 'maxChunkSize' => 10000000 ), ], 'options' => [], 'defaultUrl' => 'site/fileUpload', ]);
然后添加上传器,它扩展yiicod\fileupload\base\UploaderInterface并提供处理上传文件的功能
立即上传
class UserAvatar implement UploaderInterface { /** * Event for coco uploader * @param string $fullFileName Full file path * @param Array $userdata Userdata from widget * @param Array $results Uploaded result file * @return Array or null */ public function uploading($fullFileName, $userdata, $results) { $model = new UserModel(); //Save to temp $model->onAfterFileUploaded($fullFileName, 'logo'); //After save requered set if ($model->save()) { return [ 'url' => $model->getFileSrc('logo'), '...' => '...' ]; )else{ //Delete temp uploaded file $model->resetFile('logo'); return [ 'error' => 'Insert error message' '...' => '...' ]; }; } } }
提交时上传
class UserAvatar implement UploaderInterface{ /** * Event for coco uploader * @param string $fullFileName Full file path * @param Array $userdata Userdata from widget * @param Array $results Uploaded result file * @return Array or null */ public function uploading($fullFileName, $userdata, $results) { $model = new UserModel(); //Save to temp $model->onAfterFileUploaded($fullFileName, 'logo'); } }