简体   繁体   English

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

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

I tried to create file upload by using vich uploader integrate with fly system in symfony.我尝试使用 vich 上传器与 symfony 中的飞行系统集成来创建文件上传。

However I only can save the file name in database but not uploading.但是我只能将文件名保存在数据库中,但不能上传。

Is it something wrong with the flysystem or vich uploader setting?飞行系统或 vich 上传器设置有问题吗?

Vich Uploader setting Vich Uploader 设置

vich_uploader:
    db_driver: orm
    storage: flysystem

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

flysystem setting飞行系统设置

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

Entity实体

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

Controller Controller

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

    }

Because you are using php attributes, it looks like you may need to set the metadata.type key in your vich_uploader config ( docs )因为您使用的是 php 属性,所以看起来您可能需要在 vich_uploader 配置( 文档)中设置 metadata.type 键

vich_uploader:
    metadata:
        type: attribute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM