Upload an image to amazon S3 using VichUploaderBun

2019-05-31 05:18发布

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)

2条回答
放荡不羁爱自由
2楼-- · 2019-05-31 05:36

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);
查看更多
迷人小祖宗
3楼-- · 2019-05-31 05:44

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.

查看更多
登录 后发表回答