Answer the question
In order to leave comments, you need to log in
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>
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;
}
public $enableCsrfValidation = false;
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question