title: Im trying to upload files using ajax and symfony
body: <p>I'm using Symfony 2.7</p>
  
  <p>I got this form</p>
  
  <pre><code>&lt;form name="Document" method="post" action enctype="multipart/form-data"&gt;
      &lt;div class="col-md-6"&gt;
          &lt;input type="file" id="documento" name="Document[Ruta]" required="required"&gt;
      &lt;/div&gt;
      &lt;div class="col-md-3"&gt;
          &lt;button type="button" onclick="saveDoc()" class="btn btn-success" data-dismiss="modal"&gt;Subir&lt;/button&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>and this ajax:</p>
  
  <pre><code>function saveDoc() {
      var inputFile = document.getElementById('documento');
      var file = inputFile.files[0];
      var data = new FormData();
      data.append('doc', file, file.name);
      $.ajax({
          url: "{{ path('fileupload') }}",
          type: "POST",
          data: {'data': data},
          processData: false,
          contentType: false,
          cache: false
      });
  
  }
  </code></pre>
  
  <p>with this symfony controller:</p>
  
  <pre><code>public function fileUpload(Request $request)
  {
      $file = $request-&gt;files-&gt;get('doc')-&gt;getClientOriginalName;
      $user = $this-&gt;getUser();
  
      $filename = md5(uniqid()).'.'.$file-&gt;guessExtension();
  
      $request-&gt;files-&gt;get('doc')-&gt;move(
          $this-&gt;getParameter('upload_directory'),
          $filename
      );
      $document = new Document();
      $document-&gt;setRuta($filename);
      $document-&gt;setIdUser($user);
      $document-&gt;setFechaCreacion(new \DateTime("now"));
      $em = $this-&gt;getDoctrine()-&gt;getManager();
      $em-&gt;persist($document);
      $em-&gt;flush();
  
      return new JsonResponse(true);
  
  }
  </code></pre>
  
  <p>But I get this error:</p>
  
  <blockquote>
    <p>Notice: Trying to get property of non-object
    500 Internal Server Error - ContextErrorException</p>
  </blockquote>
  
  <p>I saw 235897 pages with file uploading with symfony and ajax, I cannot understand what is wrong in my code, can you guys help me please?</p>