我正在尝试使用VichUploaderBundle通过dropzone上传文件,当我在$media->setFile($file)
中完全分配文件时,我在控制器中收到以下错误:
Expected argument of type "string or null", "array" given
Dropzone HTML
<form action="{{path("media_dropzone")}}" class="dropzone" id="add_images_helpdesk" enctype="multipart/form-data">
<input type="hidden" name="entity" value="helpdesk" >
<input type="hidden" name="entityID" value="{{helpdesk.id}}" >
</form>
Dropzone jQuery
var myDropzone = new Dropzone("#add_images_helpdesk", {
url: $(this).attr("action"),
addRemoveLinks: true
});
我从dropzone获取ajax请求的控制器
/**
* @Route("/dropzone", name="dropzone")
* @Method({"GET", "POST"})
*/
public function dropzone(Request $request)
{
$em = $this->getDoctrine()->getManager();
$file = $request->files->get("file");
$entity = $request->request->get('entity');
$entityId = $request->request->get('entityID');
dump($file);
$media = new Media();
$media->setName("torre");
$media->setFile($file);
$media->setEntity($entity);
$media->setEntityId($entityId);
$em->persist($media);
$em->flush();
// return data to the frontend
return new JsonResponse([]);
}
我在实体中分配文件的Setter。
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\MediaRepository")
* @Vich\Uploadable
*/
class Media
{
/**
*
* @Vich\UploadableField(mapping="media", fileNameProperty="name", size="size",dimensions="dimensions")
*/
private $file;
public function setFile(?File $media = null): void
{
$this->file = $media;
if (null !== $media) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->createdAt = new \DateTime(date('Y-m-d H:i:s'));
}
}
有人可以告诉我为什么VichUploader没有处理它或它需要处理什么?