这在直接提交表单时有效。也许我没有通过我的"形式"正确反对FormData。 Laravel说"文件"没有被传递,当我在console.log(formData)时,我看到一个包含原型道具的对象,但据我所知,没有我的字段
HTML
<form enctype="multipart/form-data" accept-charset="utf-8" method="POST" action="/file">
<input id="file" type="file" name="file">
<button type="submit">Upload</button>
</form>
JS
$('.file-upload-form').submit(function (e) {
e.preventDefault();
submitUploadFileForm($(this)); //also tried just passing this without wrapper
});
function submitUploadFileForm(form){
console.log(form);
var formData = new FormData(form); //Needed for passing file
console.log(formData);
$.ajax({
type: 'post',
url: '/file',
data: formData,
success: function () {
alert('done');
},
processData: false,
contentType: false
});
}
答案 0 :(得分:4)
FormData
接受form
DOMElement,而不是jQuery对象。您只需将submitUploadFileForm()
引用传递给this
即可致电form
:
submitUploadFileForm(this);