title: Upload via Ajax in Yii2 not including files
body: <p>I'm trying to upload an image via Ajax request and seem to be hitting a similar issue as <a href="https://stackoverflow.com/questions/32055274/upload-ajax-file-in-yii2">this question</a> but it has no answer.  I'm not sure if they are exactly the same so I'll post all the details I can here and hope the more detail helps someone find an answer.</p>
  
  <p>I'm paraphrasing the below from my code (cutting out a lot of what I think is irrelevant to this question) so any typos you see are likely just me changing the code below.</p>
  
  <p>The following is included in my model:</p>
  
  <pre><code>use yii\web\UploadedFile;
  
  ...
  
  class Image extends \yii\db\ActiveRecord
  {
      public $imageFile;
  
      public function rules()
      {
          return [
              [['imageFile'], 'file', 'skipOnEmpty' =&gt; true, 'extensions' =&gt; 'png, jpg'],
          ];
      }
  
      public function upload()
      {
          if ($this-&gt;validate()) {
              $this-&gt;imageFile-&gt;saveAs('uploads/' . $this-&gt;imageFile-&gt;baseName . '.' . $this-&gt;imageFile-&gt;extension);
              return true;
          } else {
              return false;
          }
      }
  </code></pre>
  
  <p>Here is a portion of my view file:</p>
  
  <pre><code>&lt;?php $form = ActiveForm::begin(['options' =&gt; ['enctype' =&gt; 'multipart/form-data']]) ?&gt;
  &lt;?= $form-&gt;field($model, 'title') ?&gt;
  &lt;?= $form-&gt;field($model, 'imageFile')-&gt;fileInput() ?&gt;
  &lt;?= Html::submitButton('Upload', ['class' =&gt; 'btn btn-success']) ?&gt;
  &lt;?php ActiveForm::end(); ?&gt;
  </code></pre>
  
  <p>And the Ajax in the view</p>
  
  <pre><code>$('form').on('beforeSubmit', function(e) {
      var form = $(this);
      $.post(form.attr('action'), form.serialize()).done(function(result) {
          console.log(result)
      });
      return false; // Prevent default form submit action
  });
  </code></pre>
  
  <p>And the following in my controller</p>
  
  <pre><code>use yii\web\UploadedFile;
  
  ...
  
  public function actionUpload()
  {
      $model = new Image();
  
      if (Yii::$app-&gt;request-&gt;isAjax) {
          $model-&gt;load(Yii::$app-&gt;request-&gt;post());
          $model-&gt;imageFile = UploadedFile::getInstance($model, 'imageFile');
          if ($model-&gt;upload()) {
              $model-&gt;save(); // To save the title to the database
              Yii::$app-&gt;getSession()-&gt;addFlash('success', 'Image uploaded');
              return $this-&gt;redirect(['index']);
          }
      }
  
      Yii::$app-&gt;response-&gt;format = Response::FORMAT_JSON;
      return ['return' =&gt; 'just me testing here'];
  }
  </code></pre>
  
  <p>The above Ajax will only ever save the title to the database and will not upload the file.  If I transition everything to a standard post request I can get it all to work (title is saved to the database and image is uploaded to the proper directory).  If I debug around the various views, models, and controllers, it looks like I'm just not getting the imageFile via the Ajax request.  The <code>$model-&gt;load(Yii::$app-&gt;request-&gt;post());</code> loads the title that was submitted via Ajax, so why does that not also pull the file?  I thought maybe the <code>$model-&gt;imageFiles = UploadedFile::getInstances($model, 'imageFiles');</code> would be the part that pulls the Ajax submitted files but it doesn't seem to get what I need either.  As a test, I even tried treating the imageFile property as plain text and assigning it to another database property to be written and it worked just fine.  So it seems it's being included in the model properly, just not being send with the Ajax submit.</p>
  
  <p>Can anyone tell me how, in Yii2, I can submit the form over Ajax, including the selected file?</p>