繁体   English   中英

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

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

我正在使用Symfony 3.2。*

当我转储$ request时,在网页中正确选择了该文件,出现了文件名及其大小,但是该文件未放在项目的文件夹中,如何在控制器中使用该文件(文件内容我的意思是)?

在我的实体中:

/**
 * 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;

在我看来 :

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

在我的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 

在我的控制器中

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

由于您仅发布了实体类的一部分,所以这只是一个猜测。 您是否已将Uploadable注释添加到您的实体?

请参阅文档

首先,使用Uploadable注释对您的类进行注释。 这实际上就像一个标志,指示该实体包含可上传字段。

例如

<?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