/*
* 下载模块
*/
function loadapp(url,btnid,num){
mui('body').on('tap',btnid,function(){
var loadid = this.getAttribute('id');
sendloadid = loadid.slice(num);
var getvalue = document.getElementById(loadid).innerHTML;
var loaddata = {
config: {
id:sendloadid
},
fun_Success: function(data) {
console.log(JSON.stringify(data));
var geturl = data.data.down_a;
if(geturl==''){
alert("暂无数据可供下载");
}else{
createDownload(geturl,loadid);
}
}
};
if(getvalue=='下载'){
Fun_App.ExAjax(url, loaddata);
}
if(getvalue != '下载'){
document.getElementById(loadid).addEventListener('tap',function(){
document.getElementById(loadid).innerHTML ="暂停";
pauseDownload();
});
}
if(getvalue =='暂停'){
document.getElementById(loadid).addEventListener('tap',function(){
resumeDownload();
});
}
});
}
/*
* 创建下载任务
*/
function createDownload(url,loadid) {
dtask = plus.downloader.createDownload(url);
dtask.addEventListener( "statechanged", function(download,status){
var fimename = download.getFileName().slice(11).replace('.apk','');
if ( download.state == 4 && status == 200 ) {
document.getElementById(loadid).innerHTML = "下载";
plus.nativeUI.confirm( "下载已完成,是否安装?", function(e){
if(e.index==0){
openFile(download.getFileName());
}else{
del();
}
}, fimename, ["确定","取消"] );
}else{
var progress = download.downloadedSize/download.totalSize*100;
progress = parseInt(progress.toFixed(2));
if(isNaN(progress)){
document.getElementById(loadid).innerHTML = "下载";
}else if(progress == 100){
del();
document.getElementById(loadid).innerHTML = "下载";
}else{
document.getElementById(loadid).innerHTML = progress + '%';
}
}
}, false );
dtask.start();
}
/*
* 暂停下载任务
*/
function pauseDownload() {
dtask.pause();
}
/*
* 恢复下载任务
*/
function resumeDownload() {
dtask.resume();
}
/*
* 取消下载
*/
function del(){
plus.downloader.clear();
}
/*
* 打开文件操作
*/
function openFile(fileName) {
plus.runtime.openFile(fileName);
}
![tuchaofu](https://img-cdn-tc.dcloud.net.cn/account/identicon/3bea867a0834445171bc5ecc347a7f8f.png)
tuchaofu
- 发布:2016-04-27 15:50
- 更新:2016-04-27 15:50
- 阅读:3934
分享个下载apk文件,并打开的js
分类:HTML5+
0 个回复