FormData 数据
提交的数据通过FormData来进行设置 , 新版的只有支持HTML 5 的浏览器才行
方法
append key/value
formData.append("name", name);
formData.append("size", file.size);
formData.append("versionDesc", versionDesc);
formData.append("versionDescError", versionDescError);
formData.append("apkFileData", file);
实例
// 初始化
var xhr = new XMLHttpRequest();
// 进度条
xhr.upload.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
var percentComplete = Math.round(evt.loaded * 100 / evt.total);
if (percentComplete.toString() == "100") {
$("#jdt").html("");
$("#jdt").css("width", "0");
} else {
$("#jdt").html(percentComplete.toString() + '%');
$("#jdt").css("width", parseInt(percentComplete.toString()) + "%");
}
}else {
alert("unable to compute");
}
}, false);
// 请求成功
xhr.addEventListener("load", function (evt) {
$("#saveVersionDiv").prop("disabled", false);
var json = evt.target.responseText;
var parse = JSON.parse(json);
var route = resultRoute(parse);
if (route != null) {
versionVue.fileFlag = false;
ts("添加成功");
versionTable.bootstrapTable("refresh", {
silent: true
});
versionVue.closeFormDiv();
}
}, false);
// 请求错误
xhr.addEventListener("error", function (evt) {
alert("There was an error attempting to upload the file.");
}, false);
// 请求终止
xhr.addEventListener("abort", function (evt) {
alert("The upload has been canceled by the user or the browser dropped the connection.");
}, false);
// 设置请求路径:
xhr.open("POST", "/api/v1/version/saveVersion");
// 发送数据
xhr.send(formData);