[英]Using Symfony2, how to get ajax post data from the request?
I want to upload a file with jquery-file-upload (blueimp) with some data in cross domain. 我想使用跨域的某些数据上传带有jquery-file-upload(blueimp)的文件。
So in client side I use the code from basic.html file from jqueryfileupload. 因此,在客户端,我使用来自jqueryfileupload的basic.html文件中的代码。 I just added forceIframeTransport for cross domain (doc here )& formData (for format type doc here ).
我刚刚为跨域( 在此处 doc)和formData(对于doc 此处的格式类型)添加了forceIframeTransport。
var idResp = {name:'id-resp',value: SLjQuery('#media_answer_answer').data('id-resp')};
var dataSup = $('form#answer_process_form').serializeArray();
dataSup.push(idResp);
$(function () {
'use strict';
$('#media_answer_answer').fileupload({
url: "mywebsite.dev/app_dev.php/api/media/questionnaire-test-media/uploads",
dataType: 'json',
formData: dataSup,
forceIframeTransport: true,
done: function (e, data) {
console.log('upload ok ', data);
},
progressall: function (e, data) {
console.log(data.loaded/data.total);
}
}).prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled')
;
$('#media_answer_answer').fileupload(
'option',
'redirect',
'mywebsite.dev/result.html?%s'
);
});
When I select a file by the input, into the console, the log of progressall is to 1. So it seems to be sending. 当我通过输入选择文件时,进入控制台时,progressall的日志为1。因此它似乎正在发送。 Moreover I can see in the network tab in the console the request sent
此外,我可以在控制台的“网络”选项卡中看到发送的请求
//in request headers
Content-Length: 43463
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvU5EBWuRkyGkSRBS
...
//in request payload
------WebKitFormBoundaryvU5EBWuRkyGkSRBS
Content-Disposition: form-data; name="media_answer[_token]"
e6583ea5230f1f80a218825ed399115925556f0c
------WebKitFormBoundary1JHxUF9xgtcO5fJ2
Content-Disposition: form-data; name="id-resp"
420
------WebKitFormBoundaryvU5EBWuRkyGkSRBS
Content-Disposition: form-data; name="media_answer[answer]"; filename="file-to-upload.jpg"
Content-Type: image/jpeg
------WebKitFormBoundaryvU5EBWuRkyGkSRBS--
//in response headers
...
X-ChromeLogger-Data: eyJ2ZXJzaW9uIjoiNC4wIiwiY29sdW1.... // I think this is the file in base64
...
Now in server side, I use FosRestBundle for routing. 现在在服务器端,我使用FosRestBundle进行路由。 In my symfony controller I want to get the data and the file.
在我的symfony控制器中,我想获取数据和文件。
//result of request
POST /app_dev.php/api/media/questionnaire-test-media/uploads HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
...
Content-Length: 43463
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryBqSBpxJ6Wyvi2QJH
So content-length is 43463 so I think there is something, but in the controller $request->getContent()
is empty and $request->request->all()
too. 所以content-length是43463,所以我认为有些东西,但是在控制器
$request->getContent()
为空,而$request->request->all()
也是如此。
Do you know if data are sended ? 您是否知道是否发送了数据? if yes, how to get data present in payload ?
如果是,如何获取有效载荷中的数据? thanks a lot and happy new year
非常感谢,新年快乐
Finally, the logger of symfony doesn't show me anything on $request->request->all()
, but in preview tab in console I could see some var_dump on $request->request->all()
and it wasn't empty (I want to swear myself), it contains the 2 first data of request payload (content-disposition form-data), but not the last one (Content-Type: image/jpeg). 最后,symfony的记录器在
$request->request->all()
上没有显示任何内容,但是在控制台的预览选项卡中,我可以在$request->request->all()
上看到一些var_dump,而不是t空(我想发誓),它包含请求有效负载的前两个数据(内容处置形式数据),但不包含最后两个数据(内容类型:image / jpeg)。
I had to change the 2nd data to become 我不得不更改第二个数据成为
------WebKitFormBoundaryvU5EBWuRkyGkSRBS
Content-Disposition: form-data; name="media_answer[idResp]"
420
Then in the controller I can get the data like this : 然后在控制器中,我可以得到像这样的数据:
$r = $request->request->all(); // or using $request->getContent() it's also possible in a different way
$rt = $r['media_answer']['_token']; // e6583ea5230f1f80a218825ed399115925556f0c
$ri = $r['media_answer']['id-resp']; // 420
For the last data : name="media_answer[answer]" filename="file-to-upload.jpg"
As I said in the comments above I can get the file like this : 对于最后一个数据:
name="media_answer[answer]" filename="file-to-upload.jpg"
正如我在上面的评论中所说,我可以得到如下文件:
$request->files->get('media_answer[answer]', array(), true)
So for the filename : 因此,对于文件名:
$request->files->get('media_answer[answer]', array(), true)->getClientOriginalName()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.