doutan3040 2017-02-16 18:22
浏览 42
已采纳

Vichuploader文件未随帖子请求一起保存

my project is actually to create a website that permit me to post gif, jpg etc..

I'm using VichuploaderBundler to upload a file and save it to a specific repository in my app

#vich uploader Configuration
vich_uploader:
     db_driver: orm
     mappings:
        product_image:
            uri_prefix:         /images/posts
            upload_destination: '%kernel.root_dir%/../web/images/posts'
            namer:              vich_uploader.namer_uniqid
            inject_on_load:     false
            delete_on_update:   true
            delete_on_remove:   true

I'm creating an API to create and render ressources.

I'm trying to create a Post with a post request with postman. In this Post i want to save an image throw vichuploader.(this had worked with a classical form).

Here is my problem :

My post is created thx to my postman request. But the file that i upload is not saved in the

                 /**
                 * @Rest\View(statusCode=Response::HTTP_CREATED)
                 * @Rest\Post("/posts")
                 */
                public function postPostsAction(Request $request)
                {
                    $post = new Post();
                    $post->setTitle($request->get('title'))
                        ->setDescription($request->get('description'))
                        ->setImageName($request->get('image_name'))
                        ->setImageFile($request->get('imageFile'))

                        // 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
                        ->setUpdatedAt(new \DateTime());


                    $em = $this->get('doctrine.orm.entity_manager');
                    $em->persist($post);
                    $em->flush();

                    return $post;
                }

Here is my postman

Thx for helping and feedback about my post.

  • 写回答

1条回答 默认 最新

  • dongmo1708 2017-02-17 09:11
    关注

    I find how to fixed this, it was very simply in fact !

    public function postPostsAction(Request $request)
    {
        $post = new Post();
        $post->setTitle($request->get('title'))
            ->setDescription($request->get('description'))
            ->setImageName($request->get('image_name'))
            ->setImageFile($request->files->get('imageFile'))
            // 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
            ->setUpdatedAt(new \DateTime());
    
    
        // On fait appel à doctrine pour seter un id lors de la création en base
        $em = $this->get('doctrine.orm.entity_manager');
        $em->persist($post);
        $em->flush();
    
        return $post;
    }
    

    Thanks to the ->files statement my pictures are saved in the right directory

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?