使用ajax上传文件
如下是使用ajax上传文件的写法:
var formData = new FormData();
formData.append("file", $('#file')[0].files[0]);
url="<%=basePath%>user/uploadFile.do?code="+$("#code").val()";
$.ajax({
url: url,
type: 'POST',
data: formData,
async: true,
cache: false,
contentType: false,
processData: false,
dataType:"json",
success: function (returndata) {
$(top.hangge());
var returnMsg = returndata.message;
if (returnMsg.indexOf("success") == 0) {
alert(returnMsg.split("-")[1]);
top.Dialog.close();
} else {
alert(returnMsg);
}
},
error: function (returndata) {
alert("上传文件失败!");
}
});