Upload an image to amazon S3 using VichUploaderBundle

ⅰ亾dé卋堺 提交于 2019-12-23 04:04:29

问题


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)


回答1:


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.




回答2:


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);


来源:https://stackoverflow.com/questions/33103335/upload-an-image-to-amazon-s3-using-vichuploaderbundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!