Vich Uploader 与 Fly System 集成无法上传文件但保存在数据库中

Vich Uploader integrate with Fly System cannot upload file but saved in database

我尝试使用与 symfony 中的 fly 系统集成的 vich 上传器创建文件上传。

但是我只能将文件名保存在数据库中而不能上传。

是flysystem或者vich上传器设置有问题吗?

Vich 上传器设置

vich_uploader:
    db_driver: orm
    storage: flysystem

    mappings:
        uploadedfile:
            uri_prefix: /uploads
            upload_destination: uploads.storage
            namer: Vich\UploaderBundle\Naming\SmartUniqueNamer

飞行系统设置

flysystem:
  storages:
    uploads.storage:
      adapter: 'local'
      options:
        directory: '%kernel.project_dir%/public/uploads'

实体

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use App\ImportFileRepository;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

#[ORM\Entity(repositoryClass: ImportFileRepository::class)]
#[Vich\Uploadable]
class ImportFile
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private $id;

    #[ORM\Column(type: 'string', length: 255)]
    private $importFileName;

    #[Vich\UploadableField(mapping: 'uploadedfile', fileNameProperty: 'importFileName')]
    private ?File $importFile;

    #[ORM\Column(type: 'datetime', columnDefinition: 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP')]
    private $createdDate;

    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $importFile
     */
    public function setImportFile(?File $importFile = null): self
    {
        $this->importFile = $importFile;

        if (null !== $importFile) {
            $this->createdDate = new \DateTime('now');
        }

        return $this;
    }

    public function getImportFile(): ?File
    {
        return $this->importFile;
    }

    public function setImportFileName($importFileName): self
    {
        $this->importFileName = $importFileName;

        return $this;
    }

    public function getImportFileName(): ?string
    {
        return $this->importFileName;
    }

    public function getCreatedDate(): ?\DateTimeInterface
    {
        return $this->createdDate;
    }

    public function setCreatedDate(\DateTimeInterface $createdDate): self
    {
        $this->createdDate = $createdDate;

        return $this;
    }

控制器

/**
     *For build the form to twig
     * @return Response
     */
    #[Route('/import', name: 'import_index')]
    public function index(): Response
    {
        // Build form for import csv
        $form = $this->createFormBuilder()
            ->add('upload_file', VichFileType::class, [
                'required' => true,
                'allow_delete' => true,
                'asset_helper' => true,
                'attr' => ['class' => 'form-control'],
            ])
            ->getForm();

        return $this->render('/admin/import/index.html.twig', ['form' => $form->createView(),]);
    }


/**
     * For persist data and file
     * Save file by Vich Uploader integrate with FlySystem
     * @param $file
     * @return void
     */
    private function saveFile($file){
        $uploadFile = new ImportFile();

        $uploadFile->setImportFileName($file->getClientOriginalName());
        $uploadFile->setImportFile($file);

        $this->entityManager->persist($uploadFile);
        $this->entityManager->flush();

    }

因为您正在使用 php 属性,您可能需要在 vich_uploader 配置中设置 metadata.type 键 (docs)

vich_uploader:
    metadata:
        type: attribute