-->

Using the 2 amigos file uploader in yii2

2019-05-31 17:00发布

问题:

Am implementing the 2amigos file uploader it only displays the interface but does not show or upload any file

I have tried this:
<?= FileUploadUI::widget([
                'model' => $evidence,
                'attribute' => 'path',
                'url' => ['media/upload', 'id' => $evidence],
                'gallery' => false,
                '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);
                                            }',
                ],
            ]);
            ?>

回答1:

Hey Geffory only putting widget on the view we can't upload files we have to create Controller according to the widget.In my project I normally use krajee File Input yii2 extension. anyway I found an example Controller from https://github.com/2amigos/yii2-file-upload-widget/issues/5

Controller Function

public function actionUpload($id)
{
    $tour = Tour::findOne($id);
    if (!$tour) {
        throw new NotFoundHttpException(Yii::t('app', 'Page not found'));
    }
    $picture = new TourPicture(['scenario' => 'upload']);
    $picture->tour_id = $id;
    $picture->image = UploadedFile::getInstance($picture, 'image');
    if ($picture->image !== null && $picture->validate(['image'])) {

        Yii::$app->response->getHeaders()->set('Vary', 'Accept');
        Yii::$app->response->format = Response::FORMAT_JSON;

        $response = [];

        if ($picture->save(false)) {
            $response['files'][] = [
                'name' => $picture->image->name,
                'type' => $picture->image->type,
                'size' => $picture->image->size,
                'url' => $picture->getImageUrl(),
                'thumbnailUrl' => $picture->getImageUrl(TourPicture::SMALL_IMAGE),
                'deleteUrl' => Url::to(['delete', 'id' => $picture->id]),
                'deleteType' => 'POST'
            ];
        } else {
            $response[] = ['error' => Yii::t('app', 'Unable to save picture')];
        }
        @unlink($picture->image->tempName);
    } else {
        if ($picture->hasErrors(['picture'])) {
            $response[] = ['error' => HtmlHelper::errors($picture)];
        } else {
            throw new HttpException(500, Yii::t('app', 'Could not upload file.'));
        }
    }
    return $response;
}

you can use this controller function and add modify your controller according to your need..