Symfony 将 json 编码的文件对象添加到实体


Symfony add json encoded file object to entity

我正在尝试将文件对象传递给PHP,并通过以下方式使用AJAX调用将其持久保存到我的数据库中:

Js:

e.preventDefault();
    // get the file object from input
    var image = $('#profilePictureInput').prop('files')[0];
    // recreate file object
    var newImage  = {
        'lastModified'     : image.lastModified,
        'lastModifiedDate' : image.lastModifiedDate,
        'name'             : image.name,
        'size'             : image.size,
        'type'             : image.type
    };
    // convert to JSON to be able to send to controller
    var JSONimage = JSON.stringify(newImage);
    // update profilepicture
    $.ajax({
        type: 'POST',
        url: Routing.generate('uploadProfilePicture'),
        data: {
            'image' : JSONimage,
        }
    })
    .done(function (response) {
        console.log(response);
    });

我的 PHP 控制器:

$image = $this->get('request')->request->get('image');
    if ($image){
        // decode JSON object to php array
        json_decode($image);
        $em = $this->getDoctrine()->getManager();
        // insert new image object
        $image = new Image();
        $image->setFile($image);
        $em->persist($image);
        $em->flush($image);
        return new JsonResponse('success');
    }
    return new JsonResponse('false');

但是当我运行此脚本时,它返回"500 内部服务器错误"

我的问题在这里:我的 php 中有一个对象,包含如下图像值:

{"lastModified":1459104697000,"lastModifiedDate":"2016-03-27T18:51:37.000Z","name":"download (2).jpeg","size":5986,"type":"image/jpeg"}

我现在如何将 PHP 中的此对象转换为接受为实体中的文件?

帮助将不胜感激!!谢谢!

在浏览器console.log(JSONimage)中运行,就在$.ajax .您发送的不是文件,而是具有图像属性的对象。

尝试使用https://github.com/1up-lab/OneupUploaderBundle