2amigos / yii2-file-upload-widget

BlueImp File Upload Widget for Yii2
Other
252 stars 140 forks source link

Yii::$app->request->post() is empty #66

Open nlrooat opened 8 years ago

nlrooat commented 8 years ago

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!

tonydspaniard commented 7 years ago

@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