【问题标题】:How to upload .jpg file in my form Symfony 5如何以我的表单 Symfony 5 上传 .jpg 文件
【发布时间】:2021-02-08 16:04:56
【问题描述】:

我无法在 Symfony 5.1 中上传 .jpg 文件,我尝试了很多东西, 如果我在 symfony 文档 (https://www.iana.org/assignments/media-types/media-types.xhtml) 中搜索 mimtype 指南,不支持 simple.jpg,那么我该怎么做 它适用于其他文件格式。最后一次尝试: (我只想上传图片和pdf)

use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints\Image;

class CapebType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
         $builder
        ->add('name', FileType::class, [
            'label'=> 'Chargez ma carte carte Capeb',
            'required'   => true,
            'constraints' => [
                new File([
                    'maxSize' => '5M',
                    'mimeTypes' => [
                        'image/jpeg,', 'image/pjpeg', 'image/png', 'image/gif', 'image/jpg',
                        'application/pdf',
                        'application/x-pdf',
                    ],
                    'mimeTypesMessage' => 'Le fichier n\'est pas valide, assurez vous d\'avoir un fichier au format PDF, PNG, JPG, JPEG)',
                ]),
            ]
        ])
        ->add('submit', SubmitType::class, [
            'label'=> 'Envoyez ma carte CAPEB',
        ])
    ;
    }

【问题讨论】:

    标签: symfony-forms file-type symfony5 imagecreatefromjpg


    【解决方案1】:

    简单,只需要添加图片/x

    $builder
                ->add('name', FileType::class, [
                    'label'=> 'Chargez ma carte carte Capeb',
                    'required'   => true,
                    'constraints' => [
                        new File([
                            'maxSize' => '5M',
                            'mimeTypes' => [
                                'image/*',
                                'application/pdf',
                                'application/x-pdf',
                            ],
                            'mimeTypesMessage' => 'Le fichier n\'est pas valide, assurez vous d\'avoir un fichier au format PDF, PNG, JPG, JPEG)',
                        ]),
                    ]
                ])
                ->add('submit', SubmitType::class, [
                    'label'=> 'Envoyez ma carte CAPEB',
                ])
            ;
    

    【讨论】:

      猜你喜欢
      • 2011-12-09
      • 2021-07-19
      • 2016-04-18
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      相关资源
      最近更新 更多