title: Yii file upload with Ajax call?
body: <p>Hello I'm doing file upload with Yii. I have implemented this way and it works very nice:</p>
  
  <p>View:</p>
  
  <pre><code>echo CHtml::form($this-&gt;createUrl('uploadreport'), 'post', array('enctype'=&gt;'multipart/form-data'));
  &lt;div id="div_upload" class="row" style="display:none"&gt;
      &lt;?php
      $this-&gt;widget('CMultiFileUpload',array(
          'name'        =&gt; 'files',
          'accept'      =&gt; 'doc|docx',
          'max'         =&gt; 1,
          'htmlOptions' =&gt; array('size' =&gt; 25),
      ));
  
      echo CHtml::submitButton('Upload');
      ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>if (isset($_FILES['files'])) {
      $tmp_name = $_FILES['files']['tmp_name'][0];
      $filename = $_FILES['files']['name'][0];
      $new_url = '/home/ivan/reports/'.$filename;
  
      // Upload file
      move_uploaded_file($tmp_name, $new_url);
  
      $calc_id = $_POST['calc_id'];
  
      // Check if there is a previous report
      $report = Report::model()-&gt;findByAttributes(array('calc_id'=&gt;$calc_id));
      if (isset($report)) {
          // If there is a previous report, delete it
          $qtxt = "DELETE FROM `tbl_reports` WHERE `calc_id` LIKE '$calc_id';";
          $exec = Yii::app()-&gt;db-&gt;createCommand($qtxt)-&gt;execute();
      }
  
      // Insert the new report
      $rep_add = new Report;
      $rep_add-&gt;calc_id = $calc_id;
      $rep_add-&gt;report_url = $new_url;
      $rep_add-&gt;save();
  }
  </code></pre>
  
  <p>As you can see in my view I am using <code>CHtml::submitButton</code>. But I would like to use this instead:</p>
  
  <pre><code>echo CHtml::button(
      'Upload Report', array(
          'ajax'    =&gt; array(
              'type' =&gt; 'POST',
              'url'  =&gt; CController::createUrl('Calculator/uploadReport'),
          )
      )
  );
  </code></pre>
  
  <p>But if I use the above, then in my controller <code>$_FILES['files']</code> comes empty or if I try <code>CUploadedFile::getInstance();</code> it returns an empty string.</p>
  
  <p>I want to use ajax because I don't want to render any other view I want the user to remain in the current view after he/she uploads the file. How can I use Ajax or how can I stop the refreshing of the current view when I use <code>CHtml::submitButton('Upload');</code></p>
  
  <p>Any hints/help appreciated.</p>