[英]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.