使用FormData,进行Ajax请求并上传文件 html代码
js代码
function doUpload() { var formData = new FormData($( "#uploadForm" )[0]); formData .append("username", "Groucho"); formData .append("accountnum", 123456); // 数字123456被立即转换成字符串"123456" // fileInputElement中已经包含了用户所选择的文件 formData.append("userfile", fileInputElement.files[0]); $.ajax({ url: 'http://localhost:8080/cfJAX_RS/rest/file/upload' , type: 'POST', data: formData, async: false, cache: false, contentType: false, processData: false, success: function (returndata) { alert(returndata); }, error: function (returndata) { alert(returndata); } }); }