如何在没有表格的情况下上传文件
我的控制器接收发布数据.它不是来自Symfony生成的表单,而是来自使用FormData
的AngularJS自定义表单.
my controller receives post data. It's not from a Symfony generated form, but from an AngularJS custom form using FormData
.
正常参数通常在请求参数包中接收,但是如何获取上载的文件?我需要手动执行与表单handleRequest
相同的操作吗?
The normal parameters are received normally in the request parameter bag, but how do I get the file that is uploaded? Do I need to manually do the same that the form's handleRequest
does?
我的文档与烹饪书文章相同.
因此,我需要从发布请求中获取一个UploadFile,以在文档实体上调用此方法:
So I need to get an UploadFile from the post request to call this method on the document entity:
public function setFile(UploadedFile $file = null)
如何在Symfony 2中手动接收上传(不以任何方式使用表单生成器)?
How do I receive a upload manually in Symfony 2 (without using the form builder in any way)?
请求具有FileBag,类似于ParameterBag
The Request has a FileBag, similar to the ParameterBag
因此,我可以使用以下命令轻松获得指定的文件:
So I could get the file specified easily with:
$data = $this->getRequest()->request->all();
$file = $this->getRequest()->files->get('file');
并按食谱中的原样使用文档:
and use the document as is from the cookbook:
$document = new Document();
$document->setFile($file);
$lead->setDocument($document);