// 下载
downloader(downloadUrl) {
console.log(downloadUrl)
this.isDownload = true;
let dtask = plus.downloader.createDownload(downloadUrl, {}, (d, status) => {
console.log(d, status)
// 下载完成
if (status == 200) {
plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, {}, (error) => {
uni.showToast({
title: '安装失败',
mask: false,
duration: 1500
});
})
} else {
uni.showToast({
title: '更新失败',
mask: false,
duration: 1500
});
}
});
dtask.addEventListener('statechanged', (task) => {
if (!dtask) {
return;
}
// no default
switch (task.state) {
case 1:
console.log('开始下载');
break;
case 2:
console.log('链接到服务器...');
break;
case 3:
this.progressVal = Math.ceil(this.changeTwoDecimal_f(task.downloadedSize / task
.totalSize) *
100);
console.log(this.progressVal)
break;
case 4:
console.log('监听下载完成');
break;
}
});
dtask.start();
},
/**
* 例如
* 2 → 2.00
* 2.3 → 2.30
* 2.321 → 2.32
* 2.328 → 2.33
*
* */
changeTwoDecimal_f(x) {
var f_x = parseFloat(x);
if (isNaN(f_x)) {
return 0;
}
var f_x = Math.round(x * 100) / 100;
var s_x = f_x.toString();
var pos_decimal = s_x.indexOf('.');
if (pos_decimal < 0) {
pos_decimal = s_x.length;
s_x += '.';
}
while (s_x.length <= pos_decimal + 2) {
s_x += '0';
}
return s_x;
}```
uniapp 下载app的方法并且赋值进度条
最新推荐文章于 2024-05-09 11:39:57 发布