0

I'm making an app mobile and I need to upload a photo from my camera via my API. But when doctrine try to INSERT INTO, he isn't happy because my name field is null. So the name generate by my entity is missing.

I don't understand where the code block. My web app uses already the same entity photo and everything works fine.

My Api controller:

<?php
/**
 * @throws AccessDeniedException
 * @return array
 * @FOSRest\View()
 */
public function apiUploadAction()
{
    $photo = new Photo;
    $photo->setUser($this->getUser());
    $photo->setFile = new UploadedFile(
        $_FILES["file"]["tmp_name"], 
        $_FILES["file"]["name"],
        $_FILES["file"]["type"],
        $_FILES["file"]["size"],
        $_FILES["file"]["error"],
        $test = false
    );

    $em = $this->getDoctrine()->getManager();
    $em->persist($photo);
    $em->flush();

    $apiResponse = array(
        "code"      => true,
        "style"     => "success",
        /*********** SCREENSHOT COMES FROM HERE ***********/
        "message"   => $_FILES["file"]["tmp_name"]."    ".$_FILES["file"]["name"]."    ".$_FILES["file"]["type"]."    ".$_FILES["file"]["size"]."    ".$_FILES["file"]["error"],
    );

    $view = View::create();
    $view->setFormat('json');
    $view->setData($apiResponse);
    return $this->get('fos_rest.view_handler')->handle($view);
}

The $apiResponse.message (screenshot):

enter image description here

As you can see symfony has the image so the problem don't come from my app mobile. new UploadedFile(); is the right way ? To upload without form.

Hotgeart
  • 384
  • 10
  • 34
  • You can try to look at this [question](http://stackoverflow.com/questions/4083702/posting-a-file-and-data-to-restful-webservice-as-json). It provides a briefly good answer. – Hendra Huang Nov 24 '15 at 15:55

1 Answers1

0

Here is the solution :

Replace :

$photo->setFile = new UploadedFile(
        $_FILES["file"]["tmp_name"], 
        $_FILES["file"]["name"],
        $_FILES["file"]["type"],
        $_FILES["file"]["size"],
        $_FILES["file"]["error"],
        $test = false
    );

by

$photo->setFile($this->getRequest()->files->get('file'));
Hotgeart
  • 384
  • 10
  • 34