title: AJAX file upload in Play Framework 2.1 RC1 delivers an empty file
body: <p>Scala/Play gurus out there.</p>
  
  <p>I'm trying to upload a file using AJAX, in Play 2.1 (RC1). For the client part I'm using <a href="https://github.com/eldarion/bootstrap-ajax" rel="nofollow">eldarion/bootstrap-ajax</a> and everything seems to be fine, except that the uploaded file is empty.</p>
  
  <p>The front-end snippet:</p>
  
  <pre><code>...
  &lt;form action="@routes.Campaigns.upload" method="post" class="form ajax replaceable" data-replace=".replaceable"&gt;
      &lt;input type="file" name="picture"&gt;
      &lt;p&gt;&lt;input class="btn" type="submit"&gt;&lt;/p&gt;
  &lt;/form&gt;
  ...
  </code></pre>
  
  <p>Note that I had to use the explicit <code>&lt;form&gt;</code> tag instead of the <code>@form</code> helper, due to the fact that the required css class (data-replace) contains a dash, and therefore can not be used as a <code>Symbol</code>. But anyway. The called action in the controller looks like this:</p>
  
  <pre><code>  def upload = Action(parse.temporaryFile) {
      request =&gt;
        Logger.info("Trying to upload a file")
        val resultString = try {
          val file = new File("/tmp/picture")
          request.body.moveTo(file, true)
          "file has been uploaded"
        } catch {
          case e: Exception =&gt; "an error has occurred while uploading the file"
        }
        val jsonResponse = Json.toJson(
          Map("html" -&gt; Json.toJson("&lt;p&gt;" + resultString + "&lt;/p&gt;")
          )
        )
        Ok(jsonResponse)
    }
  </code></pre>
  
  <p>I'm aware that as my development goes forward the file name should be more intelligently set, but for the moment being, /tmp/picture is for me as good a name as any other one.</p>
  
  <p>The JSON response gets generated (with the "file has been uploaded" message within), and is sent back to the browser as the payload of the 200 response. The JSON is received and correctly used to modify the page (in this case, merely removing the very uploading form).</p>
  
  <p>But the file, although appearing in the right moment and in the right place, is always empty:</p>
  
  <pre><code>larsson:tmp bruno$ ls -l /tmp/picture
  -rw-r--r--  1 bruno  staff  0  7 Jan 03:07 /tmp/picture
  </code></pre>
  
  <p>That's specially strange, in my opinion, because the uploading code which uses a traditional <code>multipart/form-data</code> form, with no AJAX whatsoever, and an <code>Action</code> with <code>parse.multipartFormData</code> as a parameter, instead of <code>parse.temporaryFile</code>, works finely.</p>
  
  <p>Any help will be very appreciated. Thanks in advance.</p>