В данном случае var_dump выдает пустые массивы.
Код: Выделить всё
public function actionUpload()
{
$model = new Picture();
var_dump($_FILES);var_dump($_POST);//die();
if (Yii::$app->request->isPost)
{
$model->file = UploadedFile::getInstance($model, 'file');
if ($model->file) $model->upload();
if ($model->save())
{
echo json_encode(['success'=>true]);
return;
}
$err='';
$errs=$model->errors;
//var_dump($errs); die();
foreach ($errs as $n=>$v)
$err.="$n:$v[0]; ";
echo json_encode(['success'=>false, 'error'=>$err]);
// print_r($model->errors);
die();
//return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
Код: Выделить всё
jQuery(function(){
jQuery('#snd').click(function(){
var file=$('#file');
console.log($('#file').prop("files")[0]);
var file_data = $('#file').prop('files')[0];
var form_data = new FormData();
form_data.append('Picture[file]', '');
form_data.append('Picture[file]', file_data);
alert(form_data);
$.ajax({
url: '/medialib/picture/upload', // point to server-side PHP script
dataType: 'text', // what to expect back from the PHP script, if anything
cache: false,
contentType: 'multipart/form-data',
processData: false,
data: form_data,
type: 'POST',
success: function(php_script_response){
alert(php_script_response); // display response from the PHP script, if any
}
});
});
});
В данном контроллере для отладки $enableCsrfValidation = false;