如何在没有表格的情况下上传文件 [英] How to get file upload without a form

查看:95
本文介绍了如何在没有表格的情况下上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器接收发布数据.它不是来自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);

这篇关于如何在没有表格的情况下上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆