Symfony VichUploader:无法看到要上传的文件按钮

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Symfony VichUploader:无法看到要上传的文件按钮相关的知识,希望对你有一定的参考价值。

你好我正在测试symfony,我想使用VichUploaderBundle上传文件我试图使用VichUploader,但它不显示输入类型文件。我正在使用Symfony 3.4我已经按照与VichUploaderBundle相关的symfony网站上的教程这个图像显示了我得到的photo

代码下方

Parameters.yml

parameters:
    app.path.images: /uploads/images

Product.php

<?php

namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentHttpFoundationFileFile;
use VichUploaderBundleMappingAnnotation as Vich;

/**
 * @ORMEntity
* @VichUploadable
*/
class Product
{
/**
 * @var int
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORMColumn(type="string", length=255)
 * @var string
 */
private $image;

/**
 * @VichUploadableField(mapping="product_images", fileNameProperty="image")
 * @var File
 */
private $imageFile;

/**
 * @ORMColumn(type="datetime")
 * @var DateTime
 */
private $updatedAt;

// ...

public function setImageFile(File $image = null)
{
    $this->imageFile = $image;

    // VERY IMPORTANT:
    // 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
    if ($image) {
        // if 'updatedAt' is not defined in your entity, use another property
        $this->updatedAt = new DateTime('now');
    }
}


/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}

public function getImageFile()
{
    return $this->imageFile;
}

public function setImage($image)
{
    $this->image = $image;
}

public function getImage()
{
    return $this->image;
}


public function setUpdatedAt($updatedAt)
{
    $this->updatedAt = $updatedAt;

    return $this;
}

/**
 * Get updatedAt
 *
 * @return DateTime
 */
public function getUpdatedAt()
{
    return $this->updatedAt;
}

}

?>

config.yml

vich_uploader:
    db_driver: orm
    mappings:
        product_images:
        uri_prefix: '%app.path.images%'
        upload_destination: '%kernel.root_dir%/../web/uploads/images'

表单生成

<?php

namespace AppBundleForm;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;

class ProductType extends AbstractType
{
/**
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
    ->add('image', VichImageType::class, [
   'required' => false,
   'allow_delete' => true,
   'download_label' => '...',
   'download_uri' => true,
   'image_uri' => true,
   'imagine_pattern' => '...',])
   ->add('image')->add('updatedAt');
}/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundleEntityProduct'
    ));
}

/**
 * {@inheritdoc}
 */
 public function getBlockPrefix()
 {
    return 'appbundle_product';
 }


}
答案

您应该使用VichImageType在表单中显示图像字段,如文档中所述:https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/form/vich_image_type.md

$builder->add('image', VichImageType::class, [
       'required' => false,
       'allow_delete' => true,
       'download_label' => '...',
       'download_uri' => true,
       'image_uri' => true,
       'imagine_pattern' => '...',
]);

如果您使用EasyAdminBundle并想要集成VichUploaderBundle请确保为您的字段指定正确的类型。在这种情况下,您不需要FormType。

easy_admin:
    entities:
        Product:
            # ...
            form:
                fields:
                    - { property: 'imageFile', type: 'vich_image' }

看看这个教程:https://symfony.com/doc/master/bundles/EasyAdminBundle/integration/vichuploaderbundle.html#uploading-the-images-in-the-edit-and-new-views

以上是关于Symfony VichUploader:无法看到要上传的文件按钮的主要内容,如果未能解决你的问题,请参考以下文章

Symfony 5. EasyAdmin 3. VichUploader。同时上传多个文件

Symfony vichUploader tmp 文件不存在

VichUploader和CroppieJS:如何发送base64裁剪图像以在Symfony 4中保留

Vichuploader 破坏实体 - 意外的 EOF

Vichuploader & API 平台文件未保存

Symfony3 - 资产在生产中无法正常工作