使用
FormData()该类最简单:
因此,现在您有了一个FormData对象,准备与XMLHttpRequest一起发送。并使用FormData对象附加字段
<script type="text/javascript">$(document).ready(function () { var form = $('#forms-items'); //valid form selector form.on('submit', function (c) { c.preventDefault(); var data = new FormData(); $.each($(':input', form ), function(i, fileds){ data.append($(fileds).attr('name'), $(fileds).val()); }); $.each($('input[type=file]',form )[0].files, function (i, file) { data.append(file.name, file); }); $.ajax({ url: 'ajax/submitform.php', data: data, cache: false, contentType: false, processdata: false, type: 'POST', success: function (c) { //pre here if you want to show any success message alert(response); } }); return false; });})</script>并强制jQuery不要为您添加Content-Type标头,否则,上传文件边界字符串将丢失。



