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

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

问题描述

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

我的文档与 cookbook 文章相同.

所以我需要从 post 请求中获取 UploadFile 以在文档实体上调用此方法:

So I need to get an UploadFile from the post request to call this method on the document entity:

公共函数 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天全站免登陆