返回顶部

收藏

通过XMLHttpRequest上传文件,并显示进度条

更多
    function submitRequest(callback) {
var apkFile = document.getElementById("file").files[0];
if (apkFile) {
var fileName = apkFile.name;
var suff = fileName.substring(fileName.lastIndexOf('.') + 1,fileName.length);
if (suff != "apk") {
$.messager.alert("提示", messageCreator("文件格式必须为apk!"));
return;
}
}
var fd = new FormData();
if (apkFile) {
var apkName = apkFile.name;
fd.append("apkName", apkName);
fd.append("apkFile", apkFile);
}
var seq = $('#inputSeq').val();
fd.append("seq", seq);
fd.append("developer", $('#inputDeveloper').textbox('getText'));
fd.append("isTop", $('#inputIsTop').combobox('getValue'));
fd.append("isRecommend", $('#inputIsRecommend').combobox('getValue'));
fd.append("isRelease", $('#inputIsRelease').combobox('getValue'));
fd.append("description", $('#inputDescription').val());
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.open('POST', AppContextPath + "/rest/app/save_app", true);
/*
xhr.onload = function() {
$.messager.alert("提示", "添加成功", null, function() {
window.top.iframeNavigator('ifrmContent','service/app_main.html');
if (callback != null)
callback();

});
};
*/
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200){
$.messager.alert("提示", "添加成功", null, function() {
window.top.iframeNavigator('ifrmContent','service/app_main.html');
if (callback != null)
callback();

});
}else if(xhr.readyState == 4&& xhr.status != 200){
var jsonError = eval("(" + xhr.responseText + ")");
var errorType = jsonError.type;
if(errorType == "ApkDuplicate"){
$.messager.alert("提示", "该Apk已经上传!", null, function() {window.top.iframeNavigator('ifrmContent','service/app_main.html');

});
}
}
};
xhr.send(fd);
}
function uploadProgress(evt) {
if (evt.lengthComputable) {
var percentComplete = Math.round(evt.loaded * 100 / evt.total);
document.getElementById('progressNumber').innerHTML = '<font color=red>当前进度:'+percentComplete.toString() + '%</font>';
}
else {
document.getElementById('progressNumber').innerHTML = 'unable to compute';
}
}

标签:javascript

收藏

0人收藏

支持

0

反对

0

发表评论