I
I
Iossarian2019-03-18 14:25:45
Yii
Iossarian, 2019-03-18 14:25:45

How to properly set up the 2amigos/yii2-file-upload-widget widget?

Good afternoon. Actually, the widget itself https://github.com/2amigos/yii2-file-upload-widget
I need to set up the upload of images on the news creation page. You need to download them not along with the creation of the news, but on the viewing page. I'm using a basic preview widget template.
I have a code like this

<?php $form = ActiveForm::begin(['action' => '/payment-info/updoc','options' => ['method' => 'post']]); ?>
            <?= FileUpload::widget([
                'model' => $model,
                'attribute' => 'document',
                'url' => ['uploads/'], // your url, this is just for demo purposes,
                'options' => ['accept' => 'image/*'],
                'clientOptions' => [
                    'maxFileSize' => 2000000
                ],
                // Also, you can specify jQuery-File-Upload events
                // see: https://github.com/blueimp/jQuery-File-Upload/wiki/Options#processing-callback-options
                'clientEvents' => [
                    'fileuploaddone' => 'function(e, data) {
                                console.log(e);
                                console.log(data);
                            }',
                    'fileuploadfail' => 'function(e, data) {
                                console.log(e);
                                console.log(data);
                            }',
                ],
            ]); ?>
<?php ActiveForm::end(); ?>

Controller
public function actionUpdoc () {
        $model = new PaymentInfo();
        $directory = 'uploads' . DIRECTORY_SEPARATOR;
        $imageFile = UploadedFile::getInstance($model, 'document');
        if ($imageFile) {
            $uid = uniqid(time(), true);
            $fileName = $uid . '.' . $imageFile->extension;
            $filePath = $directory . $fileName;
            $imageFile->saveAs($filePath);
            $model->document = $filePath;  
            $path =  $directory . $fileName;
            return Json::encode([
                'files' => [
                    [
                        'name' => $fileName,
                        //'size' => $imageFile->size,
                        'url' => $path,
                        'thumbnailUrl' => $path,
                        'deleteUrl' => 'image-delete?name=' . $fileName,
                        'deleteType' => 'POST',
                    ],
                ],
            ]);
        }
    }

The file is not saved to a folder, nor is a preview of the image displayed. Here are the subtleties with urls and paths, so if anyone has worked with this widget, give parting words.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question