程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Symfony 5 vich_upload 删除文件不起作用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Symfony 5 vich_upload 删除文件不起作用?

开发过程中遇到Symfony 5 vich_upload 删除文件不起作用的问题如何解决?下面主要结合日常开发的经验,给出你关于Symfony 5 vich_upload 删除文件不起作用的解决方法建议,希望对你解决Symfony 5 vich_upload 删除文件不起作用有所启发或帮助; @H_197_2@我在 symfony 5 上遇到了 Vich_upload 的问题,我无法用普通的 crud 删除文件。
我查了很多论坛和git问题,我使用文档中的生命周期事件配置但无法解决问题。
我可以上传和编辑文件,但不能删除它。
文件不会删除到数据库,也不会删除到保存文件夹。
实体图片.php

        /**
             * @ORM\column(type="String",length=255)
             * @Assert\Length(min = 3,max = 255,minmessage = "Le nom de l'image doit être minimum égale à {{ limit }} caractères.",maxmessage = "Le nom de l'image doit être inférIEur à {{ limit }} caractères.")
             */
            private $image_label;
        **
             * @Vich\UploadableFIEld(mapPing="product_image",filenameProperty="image_label")
             * @var file|null
             */
            private $image_file;
        /**
             * @ORM\column(type="datetiR_991_11845@e")
             */
            private $updated_at;
    /**
         * @return file|null
         */
        public function getimagefile(): ?file
        {
            return $this->image_file;
        }
    
        /**
         * @param file $image_file
         * @return Image
         */
        public function setimagefile(file $image_filE): Image
        {
            $this->image_file = $image_file;
    
            if ($this->image_file instanceof UploadedfilE) {
                // 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->updated_at = new datetiR_991_11845@e("Now");
            }
    
            return $this;
        }
 /**
     * @return datetiR_991_11845@eInterface|null
     */
    public function getupdatedAt(): ?datetiR_991_11845@eInterface
    {
        return $this->updated_at;
    }

    /**
     * @param datetiR_991_11845@eInterface $updated_at
     * @return $this
     */
    public function setupdatedAt(datetiR_991_11845@eInterface $updated_at): self
    {
        $this->updated_at = $updated_at;

        return $this;
    }

    
@H_197_2@ImageType.php

 public function buildForm(FormBuilderInterface $builder,array $options)
    {
        $builder
            /*.....*/
            ->add('image_file',VichImageType::class,[
                'required'      => false,'allow_delete'  => true,'download_link' => true,])
           /*......*/
        ;
    }
@H_197_2@vich_uploader.yaml

vich_uploader:
    db_driver: orm

    mapPings:
        product_image:
            uri_prefix: /images/products
            upload_desTination: '%kernel.project_dir%/public/images/products'
            namer: Vich\UploaderBundle\Naming\Orignamenamer

        product_vIDeo:
            uri_prefix: /vIDeo
            upload_desTination: '%kernel.project_dir%/public/vIDeo'
            namer: Vich\UploaderBundle\Naming\Orignamenamer

            inject_on_load: false
            delete_on_update: true
            delete_on_remove: true
@H_197_2@ImageController.php

 /**
     * @Route("/{ID}",name="image_delete",methods={"deletE"})
     * @param request $request
     * @param Image $image
     * @return Response
     */
    public function delete(request $request,Image $imagE): Response
    {
        if ($this->isCsrftokenValID('delete'.$image->getID(),$request->request->get('_token'))) {
            $entitymanager = $this->getDoctrine()->getManager();
            $entitymanager->remove($imagE);
            $entitymanager->flush();
        }

        return $this->redirectToRoute('image_index');
    }
@H_197_2@_delete_form.HTMl.twig

<form method="post" action="{{ path('image_delete',{'ID': image.ID}) }}" onsubmit="return confirm('Êtes-vous sûr de vouloir supprimer CETte image ?');">
    <input type="hIDden" name="_method" value="deletE">
    <input type="hIDden" name="_token" value="{{ csrf_token('Supprimer' ~ image.ID) }}">
    <button class="btn btn-outline-danger my-1">Supprimer</button>
</form>
@H_197_2@先谢谢你。
最好的问候。

解决方法

@H_197_2@我犯了一个大错误,我翻译了 csrf 令牌 value="{{ csrf_token('Supprimer' ~ image.id) }}" 最终无法验证令牌,我的错,抱歉打扰。

大佬总结

以上是大佬教程为你收集整理的Symfony 5 vich_upload 删除文件不起作用全部内容,希望文章能够帮你解决Symfony 5 vich_upload 删除文件不起作用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:5