2017 © Pedro Peláez
 

yii2-extension yii2-fileupload

File uploader based on the jQuery-File-Upload for the Yii framework

image

yiicod/yii2-fileupload

File uploader based on the jQuery-File-Upload for the Yii framework

  • Friday, June 15, 2018
  • by lexxorlov
  • Repository
  • 1 Watchers
  • 5 Stars
  • 525 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 40 % Grown

The README.md

Yii File uploader based on blueimp jquery-file-upload

Latest Stable Version Total Downloads Scrutinizer Code QualityCode Climate, (*1)

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)

Install:

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)

Config

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'),            
        ],
    ];
}

Usage

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)

Upload immediately

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'
                    '...' => '...'
                ];
            };
        }
    }
}

Upload on submit

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');
    }
}

The Versions

15/06 2018

dev-master

9999999-dev

File uploader based on the jQuery-File-Upload for the Yii framework

  Sources   Download

MIT

The Requires

 

The Development Requires