user3425344
user3425344

Reputation: 3557

Upload an image to amazon S3 using VichUploaderBundle

I am using VichUploaderBundle to upload images to AmazonS3 in symfony-2.

I have followed this documentation https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/index.md and created my entity class.

So I have a setter method in the entity

/**
 * @param UploadedFile $file
 */
public function setFile(File $file = null)
{
    $this->file = $file;
    $this->updated = new \DateTime();
}

The client (This is a web application) will be sending the image in base_64 format. So I dont understand how will i get a FILE object out of that string? (Since the parameter of setter method is FILE)

Upvotes: 1

Views: 1886

Answers (2)

Jonny
Jonny

Reputation: 2333

You can use the UploadedBase64EncodedFile class from the hshn/base64-encoded-file library:

use Hshn\Base64EncodedFile\HttpFoundation\File\Base64EncodedFile;
use Hshn\Base64EncodedFile\HttpFoundation\File\UploadedBase64EncodedFile;

$uploadedFile = new UploadedBase64EncodedFile(
    new Base64EncodedFile($base64String)
);
$uploadableEntity->setFile($uploadedFile);

Upvotes: 1

Nada_Surf
Nada_Surf

Reputation: 616

Ok so first you need to save the image to a temporary file - here is a solution for that: How to save a PNG image server-side, from a base64 data string

Then you need to create an instance of an UploadedFile using that file, kind of like this example: https://github.com/dustin10/VichUploaderBundle/issues/203

And then send this UploadedFile to your setFile() method.

Upvotes: 0

Related Questions