X
X
XciloG2015-06-30 11:26:47
Java
XciloG, 2015-06-30 11:26:47

Bad Request error (#400): Unable to verify your data submission. when uploading files using the bootstrap-fileinput extension?

Installed the extension
In the view added the code

<input id="input-ru-1" type="file" multiple class="file-loading">
            <script>
                $("#input-ru-1").fileinput({
                    language: "ru",
                    uploadUrl: "index.php?r=site/upload&id=1",
                    allowedFileExtensions: ["jpg"]
                });
            </script>


In the Action controller wrote

public function actionUpload()
    {


            $model = new Images();

            if (empty($_FILES['file_data'])) {
                $output = ['error'=>'No files found for upload.'];
                return $this->render('upload', ['message'=> $output]);
            }

            //$images = $_FILES['file_data'];


            $success = null;
            $paths= [];

            $filename = md5(uniqid()) . ".jpg";
            $target = "uploads" . DIRECTORY_SEPARATOR . $_GET['id'] . DIRECTORY_SEPARATOR . $filename ;

            if(move_uploaded_file($_FILES['file_data']['tmp_name'], $target)) {
                $success = true;
                $paths[] = $target;
            } else {
                $success = false;
            }

            if ($success === true) {

                $model->created_at = time();
                $model->updated_at = time();

                $model->type = $_GET['id'];

                $model->file_name = $filename;

                if ($model->save()) {$output = [];} else  {$output = ['error'=>'Ошибка сохранения в БД'];}

            } elseif ($success === false) {
                $output = ['error'=>'Error while uploading images. Contact the system administrator' .$target];
                foreach ($paths as $file) {
                    unlink($file);
                }
            } else {
                $output = ['error'=>'No files were processed.'];
            }

       //      return $this->render('upload', ['message'=>$output]);

                Yii::$app->response->format = 'json';
                return $output;
        }


Error Bad Request (#400): Unable to verify your data submission when uploading files.

Everything works if you disable CSRF checking in the controller, but this is not the best option. How to transfer CSRF to the controller along with files I can't understand?
public $enableCsrfValidation = false;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Egor, 2018-10-15
@RATlius

Rebuild the project
how to do

X
XciloG, 2015-07-02
@XciloG

I figured out, maybe it will help someone, this parameter is passed through uploadExtraData

$("#input-ru-1").fileinput({
                    language: "ru",
                    uploadUrl: "index.php?r=site/upload&id=1",
                    allowedFileExtensions: ["jpg"],
                    uploadExtraData: {_csrf: '<?=Yii::$app->request->getCsrfToken()?>'}
                });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question