Symfony API平台:更新时上传文件

问题描述 投票:0回答:1

我在使用API​​平台和Vich Uploader进行PUT请求时遇到了麻烦,POST正常运行。

这是我的MediaObject实体标题:

/**
 * @ORM\Entity
 * @ApiResource(
 *     iri="http://schema.org/MediaObject",
 *     normalizationContext={
 *         "groups"={"media_object_read"}
 *     },
 *     collectionOperations={
 *         "post"={
 *             "controller"=CreateMediaObjectAction::class,
 *             "deserialize"=false,
 *             "security"="is_granted('ROLE_USER')",
 *             "validation_groups"={"Default", "media_object_create"},
 *             "openapi_context"={
 *                 "requestBody"={
 *                     "content"={
 *                         "multipart/form-data"={
 *                             "schema"={
 *                                 "type"="object",
 *                                 "properties"={
 *                                     "file"={
 *                                         "type"="string",
 *                                         "format"="binary"
 *                                     }
 *                                 }
 *                             }
 *                         }
 *                     }
 *                 }
 *             }
 *         },
 *         "get",
 *     },
 *     itemOperations={
 *         "get",
 *         "put"={"controller"=UpdateMediaObjectAction::class,
 *             "deserialize"=false,
 *             "security"="is_granted('ROLE_USER')",
 *             "validation_groups"={"Default", "media_object_update"},
 *             "openapi_context"={
 *                 "requestBody"={
 *                     "content"={
 *                         "multipart/form-data"={
 *                             "schema"={
 *                                 "type"="object",
 *                                 "properties"={
 *                                     "file"={
 *                                         "type"="string",
 *                                         "format"="binary"
 *                                     }
 *                                 }
 *                             }
 *                         }
 *                     }
 *                 }
 *             }},
 *     "delete"={
 *          "security"="is_granted('ROLE_USER')"
 *     }
 *     }
 * )
 * @Vich\Uploadable
 */
class MediaObject
{...}

大张旗鼓地抛出错误,因为该文件未附加到请求中。这很奇怪,因为我具有完全相同的文件输入字段,仅添加了ID参数。

有人成功做到了吗?

symfony api-platform.com
1个回答
0
投票

由我自己找到解决方案!

最后,问题是PHP在PUT上的FormData中不能很好地处理文件,而在POST上则不能很好地处理。

因此,如果遇到相同的问题,请从itemOperations的“ put”部分更改为“ method” =“ POST”。

© www.soinside.com 2019 - 2024. All rights reserved.