2amigos / yii2-file-upload-widget

BlueImp File Upload Widget for Yii2
Other
252 stars 140 forks source link

BlueImp File Upload Widget for Yii2 controller #121

Open vv-kozak opened 7 years ago

vv-kozak commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago
  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 commented 7 years ago

Can you help me?