在 yii2 中使用 2 amigos 文件上传器 [英] Using the 2 amigos file uploader in yii2

查看:35
本文介绍了在 yii2 中使用 2 amigos 文件上传器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现 2amigos 文件上传器,它只显示界面但不显示或上传任何文件

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

推荐答案

Hey Geffory 只把widget放在view上我们不能上传文件我们必须根据widget创建Controller.在我的项目中我通常使用krajee File Inputyii2 扩展.无论如何,我从 https://github.com/2amigos/找到了一个示例控制器yii2-file-upload-widget/issues/5

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

控制器功能

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..

这篇关于在 yii2 中使用 2 amigos 文件上传器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆