简体   繁体   中英

FOSRestBundle : Upload an image using json

I have been working on FOSRestBundle for some times now. I got the hang of it (forms, validation, documentation, url...). I would like to be able to upload an image for a User using json.

I think I have done everything needed on the symfony side, it works great using a form. I tried to find out how to specify an image using json in some previous post, but I wasn't succefull trying it.

To upload an image using the form:

1 - I get the image

2 - Move it in the right directory

3 - Add the unique name of the file in the Database

In my user entity class I have two variables one classical "string" and one "file":

@Assert\File (maxSize="6000000", mimeType={"image/jpeg", "image/png", "image/gif"})

In my formType I have :

->add('file', 'file')

I think I just need to know how to specify the file information and data in json using symfony2 and FOSRestBundle

Does anyone have an idea?

edit :

I have been searching around and I found the class:

Symfony\Component\HttpFoundation\File\UploadFile 

and

Symfony\Component\HttpFoundation\File\File

But I still can't make it work, anyone as a clue to help me work this out?

The easiest way is to user base64_encode and decode functions combine with file_get_contents and file_put_contents($this->getAbsolutePath(), $decoded);

Let me know if you have issue regarding this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM