简体   繁体   中英

How to store a file uploaded with vichUploaderBundle?

I'm working with Symfony 3.2.*

The file is correctly selected in the web page, when I dump $request, file name and its size appear, but the file is not put in a folder in my project, how can I use in my controller to get this one (the file content I mean) ?

In my entity :

/**
 * NOTE: This is not a mapped field of entity metadata, just a simple property.
 *
 * @Vich\UploadableField(mapping="product_image", fileNameProperty="imageName", size="imageSize")
 *
 * @var File
 */
private $imageFile;

/**
 * @ORM\Column(type="string", length=255)
 *
 * @var string
 */
private $imageName;

In my view :

 <form class="form-horizontal" action="{{ path('thepouk_admin_gestion_themes_enreg_publier') }}" method="post" enctype="multipart/form-data"> <div class="form-group"> <label class="col-md-4 control-label" for="filebutton">Image</label> <div class="col-md-4"> <input id="filebutton" name="image" class="input-file" type="file"> </div> </div> <button id="singlebutton" name="nouveau_publier" class="btn btn-success"> ADD </button> </form> 

In my config.yml :

 vich_uploader: db_driver: orm mappings: product_image: uri_prefix: /images/products upload_destination: '%kernel.root_dir%/../web/images/products' inject_on_load: false delete_on_update: true delete_on_remove: true 

In my Controller

 public function saveNewThingAction(Request $request){ $th = new Thing(); $theme->setImageFile($request->request->get('image')); $theme->setImageName($request->request->get('image')); $em = $this->getDoctrine()->getManager(); $em->persist($th); $em->flush(); return $this->render('PRBundle:Things:new.html.twig'); } 

As you have posted only parts of your entity class this is just a guess. Have you added the Uploadable annotation to your entity?

See the documentation :

First, annotate your class with the Uploadable annotation. This is really like a flag indicating that the entity contains uploadable fields.

eg

<?php

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity
 * @Vich\Uploadable
 */
class Product
{
...
}

您可以使用文件方法触摸请求对象。

$request->files->get('fileFieldName');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM