Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

BlueImp File Upload Widget for Yii2 controller #121

Open
vv-kozak opened this issue Aug 31, 2017 · 3 comments
Open

BlueImp File Upload Widget for Yii2 controller #121

vv-kozak opened this issue Aug 31, 2017 · 3 comments

Comments

@vv-kozak
Copy link

Good day.
Already the second day I try to fix the work of the widget, but I can not understand it at all
Can someone help with the controller's working code?

@tonydspaniard
Copy link
Member

What is that you need @ssvdreyf ? Can you provide the scenario, the issues that you have and what is that you require? Include code too please.

@vv-kozak
Copy link
Author

vv-kozak commented Sep 1, 2017

  1. In the form I add the following widget code:

use dosamigos\fileupload\FileUploadUI;

and

<?= FileUploadUI::widget([
        'model' => $model,
        'attribute' => 'imageFile',
        'url' => ['payments/upload', 'id' => $tour_id],
        'gallery' => true,
        'fieldOptions' => [
            'accept' => 'image/*'
        ],
        'clientOptions' => [
            'maxFileSize' => 2000000
        ],
        // ...
        'clientEvents' => [
            'fileuploaddone' => 'function(e, data) {
                                console.log(e);
                                console.log(data);
                            }',
            'fileuploadfail' => 'function(e, data) {
                                console.log(e);
                                console.log(data);
                            }',
        ],
    ]); ?>

Next, Controller:

public function actionUpload()
{
    $model = new Payments();

    $imageFile = UploadedFile::getInstance($model, 'imageFile');

    $directory = Yii::getAlias('@frontend/web/img/temp') . DIRECTORY_SEPARATOR . Yii::$app->session->id . DIRECTORY_SEPARATOR;
    if (!is_dir($directory)) {
        FileHelper::createDirectory($directory);
    }

    if ($imageFile) {
        $uid = uniqid(time(), true);
        $fileName = $uid . '.' . $imageFile->extension;
        $filePath = $directory . $fileName;
        if ($imageFile->saveAs($filePath)) {
            $path = '/img/temp/' . Yii::$app->session->id . DIRECTORY_SEPARATOR . $fileName;
            return Json::encode([
                'files' => [
                    [
                        'name' => $fileName,
                        'size' => $imageFile->size,
                        'url' => $path,
                        'thumbnailUrl' => $path,
                        'deleteUrl' => 'image-delete?name=' . $fileName,
                        'deleteType' => 'POST',
                    ],
                ],
            ]);
        }
    }
}

And model:

public $imageFile;
[['imageFile'], 'file', 'extensions'=>'jpg, gif, png', 'maxFiles' => 10],

I'm trying to upload to 1 file, but ideally I need a few.....
Help please understand me...

@vv-kozak
Copy link
Author

vv-kozak commented Sep 5, 2017

Can you help me?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants