title: Symfony2 posting a file through REST API
body: <p>I'm building a REST api using Symfony2, FOSRestBundle and JMSSerializerBundle.
  I have an URL that I use to change a user's avatar.</p>
  
  <p><code>/api/users/{id}/avatar.json</code></p>
  
  <p>I want to send an image to that URL to modify the avatar, but I get this error</p>
  
  <pre><code>"The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is  a(n) string
  </code></pre>
  
  <p>Here is my form type</p>
  
  <pre><code>class ChangeAvatarFormType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('picture', 'file')
          ;
      }
  
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'AppBundle\Entity\User',
              'csrf_protection' =&gt; false,
          ));
      }
  
      // BC for SF &lt; 2.7
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $this-&gt;configureOptions($resolver);
      }
  
      public function getName()
      {
          return 'api_user_change_avatar';
      }
  }
  </code></pre>
  
  <p>Here is my controller</p>
  
  <pre><code>    /**
       * @Rest\Put("/{id}/avatar", requirements={"id" = "\d+"})
       * @Security("has_role('ROLE_CONTRIBUTOR')")
       * @Rest\View
       */
      public function putUserAvatarAction(Request $request, $id)
      {
          $userManager = $this-&gt;get('fos_user.user_manager');
          $user = $userManager-&gt;findUserBy(array('id' =&gt; $id));
  
          $form = $this-&gt;createForm(new ChangeAvatarFormType(), $user, array('method' =&gt; 'PUT'));
          $form-&gt;handleRequest($request);
  
          if ($form-&gt;isValid()) {
  
              $userManager-&gt;updateUser($user);
  
              return new Response('', 204);
          } else {
              return $this-&gt;view($form, 400);
          }
      }
  </code></pre>
  
  <p>And here is my POSTMAN configuration:
  <img src="https://i.stack.imgur.com/jYWOo.png" alt="enter image description here"></p>
  
  <p>I have tried using a POST method too, instead of PUT.
  The file is sent to the server, it appears when I var_dump <code>$_FILES</code>
  I don't understand why the binding is not working as expected.</p>