Search code examples
yiiuploadblueimpyii-xupload

yii xupload mysql for CActiveForm


I am trying to include xupload extension for yii in the CActiveForm I try to follow this http://www.yiiframework.com/wiki/395/additional-form-data-with-xupload/ However I cannot make it. I have a form which has the field of username and filename (allow user to upload and view it from the view.) this is my view

<?php
$form = $this->beginWidget('CActiveForm', array(
      'id' => 'form-form',
      'enableAjaxValidation' => false,
        //This is very important when uploading files
      'htmlOptions' => array('enctype' => 'multipart/form-data'),
    ));
  ?>    
    <div class="row">
        <?php echo $form->labelEx($model,'username'); ?>
        <?php echo $form->textField($model,'username'); ?>
        <?php echo $form->error($model,'username'); ?>
    </div>
    <!-- Other Fields... -->
    <div class="row">
        <?php echo $form->labelEx($model,'attachment_photo'); ?>
        <?php
    $this->widget('xupload.XUpload', array(
        'url' => Yii::app()->createUrl("form/uploadAdditional", array("parent_id" => 1)),
        'model' => $model,//An instance of our model
        'attribute' => 'file',
        'multiple' => true,
        //Our custom upload template
        'uploadView' => 'application.views.site.upload',
        //our custom download template
        'downloadView' => 'application.views.site.download',
        'options' => array(//Additional javascript options
            //This is the submit callback that will gather
            //the additional data  corresponding to the current file
            'submit' => "js:function (e, data) {
                var inputs = data.context.find(':input');
                data.formData = inputs.serializeArray();
                return true;
            }"
        ),
    ));
    ?>

Solution

  • The important thing is what your $_POST array looks like. (Check this with Firebug, or Fiddler).

    Your $_POST should look like this:

    array (
      'form-form' => array ( /* stuff for additional form data */)),
      'XupLoad' => array ( /* stuff pertaining to XuLoad */)),
    )
    

    In this way, the additional form data is separate from the XupLoad data. The action as included in the Xupload extension handles the $_POST['XupLoad'] part of the array. You will have to write code to handle the $_POST['form-form'] part of the array.

    By the way, Blueimp's jQuery File Upload has a new version with important improvements and bug-fixes. It does not look like the XupLoad extension includes this latest version.

    I would suggest going with Blueimp's latest version. It has an UploadHandler class that takes care of everything server-side. The latest version is really well written. You will still have to add code for handling the additional form data...

    afterthought: It looks like you are nesting your widgets. I am not sure that this could work. Check the page source in your browser. Make sure that the HTML generated is sound. It could be that you are generating nested forms, and I have my doubts about how those would work.