上传文件时显示进度条的代码

1.代码示例如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>



<script type="text/javascript">
//下载时检测网络环境并显示下载进度
if(/android/i/.test(navigator.userAgent)) {//判断为android


}else if(/ipad | iphone/i.test(navigator.userAgent)){//判断为ios
//移除按钮
} else {
//pc端访问,移除按钮 
}
mui.plusReady(function(){
//获取当前网络状态的函数
function GetNetWorkState() {
var NetStateStr = '未知';
var types = {};
types[plus.networkinfo.CONNECTION_UNKNOW] = '未知';
types[plus.networkinfo.CONNECTION_NONE] = '未连接网络';
types[plus.networkinfo.CONNECTION_ETHERNET] = '有线网络';
types[plus.networkinfo.CONNECTION_WIFI] = 'WiFi网络';
types[plus.networkinfo.CONNECTION_CELL2G] = '2G蜂窝网络';
types[plus.networkinfo.CONNECTION_CELL3G] = '3G蜂窝网络';
types[plus.networkinfo.CONNECTION_CELL4G] = '4G蜂窝网络';
NetStateStr = types[plus.networkinfo.getCurrentType()];

return NetStateStr;
};
//创建下载任务
window.createDownload() {

if(GetNetWorkState()=="未连接网络" || GetNetWorkState()=="未知") {
plus.nativeUI.alert("网络状况不佳,请检查网络设置");
return;
}else if(GetNetWorkState()=="有线网络" || GetNetWorkState()=="3G蜂窝网络" || GetNetWorkState()=="2G蜂窝网络" || GetNetWorkState()=="4G蜂窝网络") {
mui.confirm('非wifi环境下确定下载?','提示',['取消','确定'], function(e) {
if(e.index == 1) {
startDownload();
}
})
}else{
startDowanload();
}
}

//下载并显示下载进度的函数
function startDownload() {
document.getElementById("jindu").style.display = "block";
document.getElementById("download").innerHTML = "安装中";
var dtask = plus.downloader.createDownload("utrl",{},function (d,status) {
//下载完成
if(status == 200) {
d.filename = d.filename;
document.getElementById("download").onclick = function() {
plus.runtime.openFile(d.filename);
}
} else {
plus.nativeUI.alert("下载失败!");
}
});
//调用显示进度的函数
range("url");
dtask.start();
}

//取消下载任务
document.getElementById('gameOver').onclick = function () {

}

var req = new XMLHttpRequest();
function range(url) {
var percentComplete;
req.addEventListener("progress", function(event) {
if(event.lengthComputable) {
percentComplete = parseFloat((event.loaded / event.total) * 100).toFixed(0);
document.getElementById('jindu').innerHTML = "(" + percentComplete +"%)";
if(document.getElementById("jindu").innerHTML == "(100%)") { 
document.getElementById("download").innerHTML = "打开"; 
   document.getElementById("gameOver").style.display = "none";
 
}
}
},false);
req.open("GET",url);
req.send();
}
function stop() {
req.abort(); //终止XMLHttpRequest对象
window.stop();

}

});

</script> 
</body>
</html>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值