jQuery 使用 ajaxForm 插件提交 FormData
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19441271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Submit FormData with ajaxForm plugin
提问by paugoo
How to put FormData object into a form and submit it by using ajaxForm plugin? I'm trying to do something as below:
如何将 FormData 对象放入表单并使用 ajaxForm 插件提交?我正在尝试执行以下操作:
var fd = new FormData();
fd.append('photo', file);
var form = $("<form method='POST' action='url.php' enctype='multipart/form-data'></form>");
add the fd formData as the object to the form and convert the form into ajaxForm by:
将 fd formData 作为对象添加到表单中,并通过以下方式将表单转换为 ajaxForm:
form.ajaxForm({
beforeSend: function(e) {},
uploadProgress: function(event, position, total, percentComplete) {},
complete: function(data) {}
});
then submit the form. So in the url.php I can fetch the file by $_FILES['photo'];
然后提交表格。所以在 url.php 中,我可以通过 $_FILES['photo']; 获取文件。
回答by shiva
You can directly send data as like normal ajax post..
您可以像普通的ajax post一样直接发送数据..
Like ..
喜欢 ..
form.ajaxForm({
data: { data: fd },
beforeSubmit:function() {
}
beforeSend: function(e) {},
uploadProgress: function(event, position, total, percentComplete) {},
complete: function(data) {}
});
I have tested it and it is working fine..
我已经测试过了,它工作正常..
回答by Sem
That's what is working for me
这就是为我工作的
var fd = new FormData(this);
fd.append('upload', file);
$(this).ajaxSubmit({
formData: fd,
...
});