2017-02-16 101 views
0

我的項目實際上是建立一個網站,允許我發佈GIF,JPG等。Vichuploader文件不保存與POST請求

我使用VichuploaderBundler上傳文件並將其保存到特定的資源庫在我的應用程序

#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 

我創建了一個API來創建和渲染ressources。

我正在嘗試創建一個帖子,提供郵遞員的郵寄請求。 在這篇文章中,我想保存一個圖像扔vichuploader。(這曾經與一個古典形式)。

這裏是我的問題:

我的職位是創建THX我郵遞員請求。但是,我上傳的文件不會被保存在

    /** 
       * @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幫助和反饋有關我的文章。

回答

1

我發現如何解決這個問題,其實很簡單!

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

得益於 - >文件聲明我的照片保存在正確的目錄