[英]Upload files with ajax and Symfony2
I've problem with upload file from form to Symfony2 controller by ajax. 我有问题从ajax从表单上传文件到Symfony2控制器。
This is my form by client side: 这是客户方的表格:
var uploadFile = function() {
var content = "<form id='uploadFile' enctype='multipart/form-data' action='' method='post'>" +
"<input id='file' type='file'/>" +
"</form>";
$("#upload-dialog").html(content);
$("#upload-dialog").dialog({
resizable: false,
title: 'Dodaj załączniki do umowy',
height: 300,
width: 450,
buttons: [
{
text: 'Wyślij',
click: function() {
var formData = new FormData();
formData.append('file', document.getElementById('file').files[0]);
$.ajax({
url: Routing.generate('employees_upload_attachment'),
data: formData,
enctype: 'multipart/form-data',
processData: false,
contentType: false,
success: function() {
},
error: function() {
}
});
}
}
]
});
}; };
and this is my controller 这是我的控制者
public function uploadAttachmentAction(Request $request) {
$fileBag = $request->files;
var_dump($fileBag);
}
When I try to show uploaded file I get an empty FileBag object: 当我尝试显示上传的文件时,我得到一个空的FileBag对象:
object(Symfony\\Component\\HttpFoundation\\FileBag)#12 (1) {
object(Symfony \\ Component \\ HttpFoundation \\ FileBag)#12(1){
["parameters":protected]=> array(0) { } }[“parameters”:protected] => array(0){}}
What could be wrong? 可能有什么不对?
EDIT: I solved it.I added to jquery ajax parameters 编辑:我解决了。我添加到jquery ajax参数
cache: false, type: 'POST'
cache:false,输入:'POST'
and everything is ok :) 一切都很好:)
Adding ajax paramaters: 添加ajax参数:
cache: false,
type: 'POST'
Solved problem. 解决了问题。
Note: Answer is based on comment not mine because it is showing up in the "unanswered" section. 注意:答案是基于评论不是我的,因为它出现在“未答复”部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.