使用 symfony2 从 post Multipart (Content-Disposition: form-data) 中获取数据

C2d*_*ric 6 javascript php multipartform-data symfony angularjs

使用 AngularJS,我在一个请求中发布表单数据和文件。我无法在 symfony2 的控制器中正确处理数据。

我只得到一串完整的数据,$request->getContent()但没有被解析。

这是更多详细信息:

1 - 我像这个例子一样发送数据并且它有效=> http://shazwazza.com/post/Uploading-files-and-JSON-data-in-the-same-request-with-Angular-JS

2 - 这里是发送的数据:

-----------------------------23743060120122
Content-Disposition: form-data; name="model"

{"type_inventaire_id":"8","profondeur_id":"2","code_pays_elv":"FR"}
-----------------------------23743060120122
Content-Disposition: form-data; name="file"; filename="importChep.txt"
Content-Type: text/plain

69182002
22096034
22096033
00110011
//69182007
69182003

-----------------------------23743060120122
Run Code Online (Sandbox Code Playgroud)

3 - 我只能得到数据,$request->getContent()但它没有被解析我想这样得到它:

$file = $request->files->get('file');
$model = $request->get('model');
Run Code Online (Sandbox Code Playgroud)

我找了一天的解决方案,但我没有找到任何东西......

有人有想法吗?

Zeu*_*eux 4

您正在执行 POST 或 PUT 请求吗?我发现使用 PUT 方法时 symfony 不允许您访问属性,但是使用 POST 您应该能够访问您的数据:

$file = $request->files->get('file');
$model = $request->request->get('model');
Run Code Online (Sandbox Code Playgroud)