yiicod/yii2-fileupload

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

Installs: 1 011

Dependents: 0

Suggesters: 0

Security: 0

Stars: 7

Watchers: 2

Forks: 0

Open Issues: 0

Type:yii2-extension

dev-master 2018-11-03 21:35 UTC

This package is auto-updated.

Last update: 2024-04-27 00:07:11 UTC


README

Latest Stable Version Total Downloads Scrutinizer Code QualityCode Climate

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.

Install:

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yiicod/yii2-fileupload "*"

or add

"yiicod/yii2-fileupload": "*"

to the require section of your composer.json.

Config

Set to controller

public function actions()
{
    return array(
        'fileUpload' => [
            'class' => 'yiicod\fileupload\actions\FileUploadAction',
        ],
    );
}

Add behavior for model

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

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