作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 API PLatform 实现文件上传继 Documentation使用维希.但它不起作用,确切地说,MediaObject 没有被我根据请求发送的文件所填充。
我完全遵循了 API 平台提供的说明书,但我的表单似乎不能很好地处理请求,因为它没有通过约束验证,我从 API 得到了这个答案:
{
"type": "https:\/\/tools.ietf.org\/html\/rfc2616#section-10",
"title": "An error occurred",
"detail": "file: This value should not be null.",
"violations": [
{
"propertyPath": "file",
"message": "This value should not be null."
}
]
}
我的请求是基本的: header 是多部分/表单数据,我发送一个小文件(78Ko): "file"=> 图片.jpg
当我转储 $request 对象时,文件位于其中,但 $form->getData() 未水合。知道为什么吗?
这是我的 MediaObject :
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Traits\updatedAt;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Entity\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ApiResource(iri="http://schema.org/MediaObject", collectionOperations={
* "get",
* "post"={
* "method"="POST",
* "path"="/media_objects",
* "controller"=CreateMediaObjectAction::class,
* "defaults"={"_api_receive"=false},
* },
* })
* @Vich\Uploadable
* @ORM\Entity(repositoryClass="App\Repository\MediaObjectRepository")
* @ORM\HasLifecycleCallbacks()
*/
class MediaObject
{
use updatedAt;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var File|null
* @Assert\NotNull()
* @Vich\UploadableField(mapping="media_object", fileNameProperty="contentUrl")
*/
public $file;
/**
* @var string|null
* @ORM\Column(type="string", length=255, nullable=true)
* @ApiProperty(iri="http://schema.org/contentUrl")
*/
private $contentUrl;
public function getId(): ?int
{
return $this->id;
}
public function getFile(): ?string
{
return $this->file;
}
/**
* @param string $file
*
* @return MediaObject
* @throws \Exception
*/
public function setFile(string $file): self
{
$this->file = $file;
if (null !== $file) {
$this->updatedAt = new DateTimeImmutable();
}
return $this;
}
public function getContentUrl(): ?string
{
return $this->contentUrl;
}
public function setContentUrl(?string $contentUrl): self
{
$this->contentUrl = $contentUrl;
return $this;
}
}
这是我的媒体对象类型:
namespace App\Form;
use App\Entity\MediaObject;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichFileType;
class MediaObjectType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// Configure each fields you want to be submitted here, like a classic form.
->add('file', VichFileType::class, [
'label' => 'label.file',
'required' => false,
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => MediaObject::class,
'csrf_protection' => false,
]);
}
public function getBlockPrefix()
{
return '';
}
}
这是我的 Controller :
namespace App\Controller;
use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException;
use App\Entity\MediaObject;
use App\Form\MediaObjectType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Validator\ValidatorInterface;
final class CreateMediaObjectController extends AbstractController
{
private $validator;
private $doctrine;
private $factory;
public function __construct(RegistryInterface $doctrine, FormFactoryInterface $factory, ValidatorInterface $validator)
{
$this->validator = $validator;
$this->doctrine = $doctrine;
$this->factory = $factory;
}
/**
* @param Request $request
* @Route("media_objects", name="media")
*
* @return MediaObject
*/
public function mediaCreation(Request $request): MediaObject
{
$mediaObject = new MediaObject();
$form = $this->factory->create(MediaObjectType::class, $mediaObject);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->doctrine->getManager();
$em->persist($mediaObject);
$em->flush();
// Prevent the serialization of the file property
$mediaObject->file = null;
return $mediaObject;
}
// This will be handled by API Platform and returns a validation error.
throw new ValidationException($this->validator->validate($mediaObject));
}
}
最佳答案
我使用 postman 和 symfony 4.x 来回答这个问题。
API URL:localhost/api/media_objects(localhost => baseURL)
选择表单数据=> key 应为文件并选择文件类型(如果将鼠标悬停到右侧进行输入,您将看到下拉菜单)
选择文件并发布。
注意:您必须拥有 config/packages/vich_uploader.yaml 以及类似配置
vich_上传者: db_driver:orm
mappings:
media_object:
uri_prefix: /media
upload_destination: '%kernel.project_dir%/public/media'
namer: Vich\UploaderBundle\Naming\UniqidNamer
关于Symfony - API 平台 - 文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53361011/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!