Answer the question
In order to leave comments, you need to log in
Why does POST not send data to yii2 server?
Here is the form sent by Ajax:
<form id="myForm">
<input id="idmodel" type="hidden" name="idmodel" value="28254">
<input id="myFiles" name="myFiles" type="file" size="1"/>
</form>
var urlPrefix = 'http://site.com/';
var url = urlPrefix + 'dev/my-function';
$.ajax({
type: 'post',
url: url,
data: formData,
cache: false,
contentType: false,
processData: false,
xhr: function () {
var xhr = $.ajaxSettings.xhr(); // получаем объект XMLHttpRequest
xhr.upload.addEventListener('progress', function (evt) { // добавляем обработчик события progress (onprogress)
if (evt.lengthComputable) { // если известно количество байт
// высчитываем процент загруженного
var percentComplete = Math.ceil(evt.loaded / evt.total * 100);
// устанавливаем значение в атрибут value тега <progress>
// и это же значение альтернативным текстом для браузеров, не поддерживающих <progress>
// progressBar.val(percentComplete).text('Загружено ' + percentComplete + '%');
console.log('percentComplete: ', percentComplete);
}
}, false);
return xhr;
},
success: function (data) {
console.log(data);
}
});
public function actionMyFunction()
{
$key = Yii::$app->request->post();
return json_encode($_POST);
$file = $_FILES;
$id = $key['idmodel'];
$model = $this->findModel($id);
$model->myFiles = UploadedFile::getInstanceByName('myFiles');
return json_encode($model->myUpload());
if ($model->myUpload()) {
return ($model->save(false));
}
}
{"idmodel":"28254"}
[]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question