简体   繁体   English

如何存储通过vichUploaderBundle上传的文件?

[英]How to store a file uploaded with vichUploaderBundle?

I'm working with Symfony 3.2.* 我正在使用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) ? 当我转储$ request时,在网页中正确选择了该文件,出现了文件名及其大小,但是该文件未放在项目的文件夹中,如何在控制器中使用该文件(文件内容我的意思是)?

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 : 在我的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? 您是否已将Uploadable注释添加到您的实体?

See the documentation : 请参阅文档

First, annotate your class with the Uploadable annotation. 首先,使用Uploadable注释对您的类进行注释。 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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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