[英]How to get file from form in Symfony2?
I have form with File Field Type . 我有文件字段类型的表单。 I would like to "catch" uploaded file in controller but I don't know how to do it. 我想在控制器中“捕获”上传的文件,但我不知道该怎么做。
I tried to get access via $form->get('file')
, but seems to me that this is the wrong way to go. 我试图通过$form->get('file')
获得访问权限,但在我看来这是错误的方法。 Why? 为什么? var_dump( $form->get('file') );
return string with a filename (and nothing more)! 返回带有文件名的字符串(仅此而已)! I expected the object or something like that, but I got only a filename. 我期望该对象或类似的东西,但是我只有一个文件名。
Do this if you just want to get the file from the form: 如果您只想从表单中获取文件,请执行以下操作:
$myFile = $request->files->get('inputFieldFileId');
this should be all you need 这应该是您所需要的
When you set up your form and entity right symfony will handle it automatically. 设置表单和实体权限后,symfony会自动处理它。
Example set up: 设置示例:
Entity (Document has a file) 实体(文档有一个文件)
<?php
namespace Acme\DemoBundle\Entity\Document;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class Document
{
/**
* @var UploadedFile
*/
protected $file;
/**
* @param UploadedFile $file - Uploaded File
*/
public function setFile($file)
{
$this->file = $file;
}
/**
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
//add functions described here: http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html
}
FormType 表格类型
<?php
namespace Acme\DemoBundle\Entity\DocumentType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class DocumentType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', 'file')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Entity\Document'
));
}
/**
* @return string
*/
public function getName()
{
return 'acme_demo_document';
}
}
Controller snippet ( probably createAction
and updateAction
) 控制器代码段 (可能是createAction
和updateAction
)
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
\Doctrine\Common\Util\Debug::dump($entity->getFile());
$entity->getFile()
will hold the file. $entity->getFile()
将保存文件。
Take a look here: http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html for instructions on how to implement moving the file from /temp to eg web/uploads
and other things. 在这里看看: http : //symfony.com/doc/current/cookbook/doctrine/file_uploads.html,以获取有关如何实现将文件从/ temp移至例如web/uploads
。
If your IDE doesn't show you what you can do with the UploadedFile
you can look it up here: http://api.symfony.com/2.0/Symfony/Component/HttpFoundation/File/UploadedFile.html 如果您的IDE没有显示您可以使用UploadedFile
进行的操作,则可以在此处查找: http : //api.symfony.com/2.0/Symfony/Component/HttpFoundation/File/UploadedFile.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.