【问题标题】:The file "" does not exist (VichUploaderBundle)文件“”不存在 (VichUploaderBundle)
【发布时间】:2019-04-16 20:33:12
【问题描述】:

我的问题是,当我尝试使用 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'
      ]);
    }

我只是希望能够在我的文件夹中看到我的音频文件,就像图像一样,我很确定这很愚蠢,如果你们需要在我的代码中看到其他内容,请问我可以更新问题

【问题讨论】:

  • 文件顶部有你的 * @ApiResource() 文档块吗?
  • 抱歉我的无知,@ApiResource 是什么?无论哪种方式我都没有将它用于图像,它工作得很好。加上我没有使用任何 API
  • 我认为updatedAt 不见了
  • 我刚刚将该字段添加到我的实体中,但它仍然给我同样的错误,它一定是别的东西

标签: php symfony4 sonata-admin vichuploaderbundle


【解决方案1】:

我不得不改变一些东西来解决我的问题

首先,我在实体中添加了一个名为“$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,他给了我添加这些字段的想法,最终解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 1970-01-01
    相关资源
    最近更新 更多