dev-master
9999999-devFile uploader based on the jQuery-File-Upload for the Yii framework
MIT
The Requires
The Development Requires
by Alexey Orlov
File uploader based on the jQuery-File-Upload for the Yii framework
File uploader base on blueimp jquery-file-upload. You can write easy themes for uploader. This extension provide you all workflow for upload files on your server., (*2)
The preferred way to install this extension is through composer., (*3)
Either run, (*4)
php composer.phar require --prefer-dist yiicod/yii2-fileupload "*"
or add, (*5)
"yiicod/yii2-fileupload": "*"
to the require section of your composer.json., (*6)
Set to controller, (*7)
public function actions() { return array( 'fileUpload' => [ 'class' => 'yiicod\fileupload\actions\FileUploadAction', ], ); }
Add behavior for model, (*8)
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', ]);
Then add uploader, which extends yiicod\fileupload\base\UploaderInterface and provides functionality to handle uploaded file, (*9)
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'); } }
File uploader based on the jQuery-File-Upload for the Yii framework
MIT