dragam0217 2019-04-16 20:33
浏览 27
已采纳

文件“”不存在(VichUploaderBundle)

My problem is that when i try to upload an mp3 file using VichUploadBundler in combination with sonata-admin, i get the following error when i upload the file.

"The file "" was not found"

I aleady have another form inside my project where i upload some images and i have no problem with them, so im quite shock, because i thought that audio files would be easier

Here is my Entity file

/**
* 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;
}

My vich_uploader.yaml file

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

And finally my admin class

    protected function configureFormFields(FormMapper $formMapper) {
    $formMapper
      ->add('audioName')
      ->add('audioFile', VichFileType::class, [
        'label' => 'Audio'
      ]);
    }

I just want to be able to see my audio files on my folder, just like the images, im pretty sure is something silly, if you guys need to see something else in my code, just ask so i can update the question

  • 写回答

1条回答 默认 最新

  • drtoclr046994545 2019-04-17 14:34
    关注

    I had to change some things in order to fix my problem

    First of all, i added a new property in the entity called "$audioSize", like this:

     /**
     * @ORM\Column(type="integer")
     *
     * @var integer
     */
    private $audioSize;
    
    //getters and setters ...
    

    I had to change my audioFile property as well, like so...

    /**
    * 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;
    

    and finally in the project php.ini file i had to change the upload max size of the files

    post_max_size = 1024M
    upload_max_filesize = 1024M
    

    I will leave it here for future reference in case someone else encounters the same issue.

    p.d Special thanks to @Iaviku who gave me the idea of adding those fields that at the end solved my problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答