我的问题是,当我尝试使用VichUploadBundler与sonata-admin组合上传mp3文件时,我在上传文件时收到以下错误。
"The file "" was not found"
我已经在我的项目中有另一个表单,我上传了一些图像,我没有问题,所以我很震惊,因为我认为音频文件会更容易
这是我的实体文件
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="episode_audio", fileNameProperty="audioName")
*
* @var File
*/
private $audioFile;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $audioName;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime
*/
private $updatedAt;
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $audioFile
*/
public function setAudioFile(?File $audioFile = null): void
{
$this->audioFile = $audioFile;
if (null !== $audioFile) {
// 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->updatedAt = new \DateTimeImmutable();
}
}
public function getAudioFile(): ?File
{
return $this->audioFile;
}
public function setAudioName(?string $audioName): void
{
$this->audioName = $audioName;
}
public function getAudioName(): ?string
{
return $this->audioName;
}
我的vich_uploader.yaml文件
vich_uploader:
db_driver: orm
mappings:
..//
episode_audio:
uri_prefix: /audio
upload_destination: '%kernel.project_dir%/public/audio/'
delete_on_update: true
delete_on_remove: true
inject_on_load: true
最后是我的管理课
protected function configureFormFields(FormMapper $formMapper) {
$formMapper
->add('audioName')
->add('audioFile', VichFileType::class, [
'label' => 'Audio'
]);
}
我只是希望能够在我的文件夹上看到我的音频文件,就像图像一样,我非常肯定是愚蠢的,如果你们需要在我的代码中看到别的东西,只要问我可以更新问题
为了解决我的问题,我不得不改变一些事情
首先,我在名为“$ audioSize”的实体中添加了一个新属性,如下所示:
/**
* @ORM\Column(type="integer")
*
* @var integer
*/
private $audioSize;
//getters and setters ...
我不得不改变我的audioFile属性,就像这样......
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Assert\File(
* maxSize="1024M",
* mimeTypes={"audio/mpeg", "audio/mp3"}
* )
* @Vich\UploadableField(mapping="episode_audio", fileNameProperty="audioName", size="audioSize")
*
* @var File $audioFile
*/
private $audioFile;
最后在项目php.ini文件中,我不得不更改文件的上传最大大小
post_max_size = 1024M
upload_max_filesize = 1024M
如果其他人遇到同样的问题,我会把它留在这里供将来参考。
p.d特别感谢@Iaviku,他给了我添加那些最终解决了我的问题的字段的想法。