0

The problem is that I can't get the CMultifileUpload instance in my controller/action, when I call this action using ajax

here is my code in my view

<?php $form=$this->beginWidget('CActiveForm', array('id' => 'form',
    'enableAjaxValidation' => false, 'htmlOptions' => array('enctype' => 'multipart/form-data'),)); ?>

    <div class="row">
    <?php
        $this -> widget('CMultiFileUpload', array('name' => 'wupload', 'accept' => 'zip', \
        'duplicate' => 'Duplicate file!', // useful, i think
        'denied' => 'Invalid file type', // useful, i think
        'max' => 1
        ));

        echo CHtml::button('upload', array('onclick'=>'send()',));
        ?>
    </div>

<?php $this->endWidget(); ?>

here is my action in my controller

public function actionUpload(){
    $file = CUploadedFile::getInstancesByName('wupload');
    var_dump($file);
}

here is my script function

function send() {
    var data = $("#form").serialize();

    $.ajax({
        type : 'POST',
        url : 'upload',
        data : data,
        success : function(data) {
            alert(data);
        },
        error : function(data) {// if error occured
            alert("Error occured.please try again " + data);
        },

        dataType : 'html'
    });
}

now all what I receive after the alert shows is this

array(0){
}

I can't see my selected files, unlike if I used normal submit button, every thing worked correctly, I need someone tell me what am I doing wrong here?

Aamer
  • 258
  • 3
  • 14

1 Answers1

0

You need to use the FormData object (NOTE: For IE it will only work in IE10+).

Lots of examples here, There's a jQuery one at the bottom.

https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects?redirectlocale=en-US&redirectslug=Web%2FAPI%2FFormData%2FUsing_FormData_Objects

Alex
  • 2,102
  • 1
  • 13
  • 16