yii2-file-upload-widget
yii2-file-upload-widget copied to clipboard
Yii::$app->request->post() is empty
Hi,
A couple of days ago I updated the yii2-file-upload-widget extension. Before that time, everything worked fine. Since the update, I get an error when updating files.
Widget:
<?= FileUploadUI::widget([
'model' => $model,
'attribute' => 'file',
'url' => ['file/upload'],
'gallery' => false,
'fieldOptions' => [
'accept' => 'image/*'
],
'clientOptions' => [
'maxFileSize' => 2000000
],
'clientEvents' => [
'fileuploaddone' => 'function(e, data) {
console.log(e);
console.log(data);
}',
'fileuploadfail' => 'function(e, data) {
console.log(e);
console.log(data);
}',
],
]);
?>
Controller:
public function actionUpload()
{
Yii::$app->response->getHeaders()->set('Vary', 'Accept');
Yii::$app->response->format = Response::FORMAT_JSON;
$model = new UploadFile;
$post = Yii::$app->request->post();
if ($model->load($post)) {
// get the uploaded file instance. for multiple file uploads
// the following data will return an array
$file = UploadedFile::getInstance($model, 'file');
...
The $post variable above is an empty array, so that the if statement is never entered. When I use $post = $_FILES; instead of $post = Yii::$app->request->post();, everything works just fine. Why is the post request variable an empty array? What am I doing wrong here?
Thanks!
@nlrooat I think this has nothing to do with the extension. The problem is that you try to set the attributes of your UploadFile Model using the 'post'ed variables, and the correct way is that you do only this IF no other attributes are set on the form:
$model = new UploadFile;
$file = UploadedFile::getInstance($model, 'file');
if($file) {
.... whatever