vova07 / yii2-imperavi-widget

Imperavi Redactor widget for Yii 2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ajax request is empty

sinergycode opened this issue · comments

Code:
action:

public function actionNativeImperavi() {
        $this->enableCsrfValidation = false;
        $result = [
          'image-upload' => [
              'class' => 'vova07\imperavi\actions\UploadFileAction',
              'url' => 'http://site.com/public_html/uploads/images/blog/',
          ]
        ];
        return Json::encode($result);
    }

view:

    <?= $form->field($model, 'text')->widget(Widget::className(), [
        'settings' => [
            'lang' => 'ru',
            'minHeight' => 200,
            'formatting' => ['p', 'blockquote', 'h2'],
            'imageUpload' => \yii\helpers\Url::to(['site/native-imperavi']),
            'plugins' => [
                'clips',
                'fullscreen',
                'imagemanager'
            ],
        ],
    ]); ?>

Yii2 debuger:
method: 'POST'
isAjax: true
Route: 'site/native-imperavi'
Action: 'backend\controllers\SiteController::actionNativeImperavi()'
$_FILES - file [
'name' => 'aaaaa.png'
'type' => 'image/png'
'tmp_name' =>'E:\Opse\OSPanel\userdata\temp\php961C.tmp'
'error' => 0
'size' => 99568
]
Request Body: Empty.

Help me please.

Hi @sinergycode!

The problem is in your action code.
You should use the predefined action in the controller like this:

public function actions()
{
    return [
             'native-imperavi' => [
              'class' => 'vova07\imperavi\actions\UploadFileAction',
              'url' => 'http://site.com/public_html/uploads/images/blog/',
              'path' => '@alias/to/my/path'
          ]
    ];
}

Note: You should specify the path where the files will be uploaded.

The code from view didn't change.

In case you still have problems, feel free to re-open this issue.

Thanks it's working, but not enought.
The request body is also empty.

@sinergycode nope, I mean you should add the external action into your controller.
In case you wanna do it as you do you should copy-paste the code from UploadFileAction.

Let's use for example the SiteController from yii2-app-basic template.
In that controller we have such actions() method:

/**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

In your case you need to add in that method one more action, to get in final something like this:

/**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
            'native-imperavi' => [
                'class' => 'vova07\imperavi\actions\UploadFileAction',
                'url' => 'http://site.com/public_html/uploads/images/blog/',
                'path' => '@app/uploads/images/blog'
            ]
        ];
    }

After that you code should start working as expecting.

Note: In case you don't have that action in your controller, you should add it with at least one action: native-imperavi.