1.安装
cordova plugin add cordova-plugin-file-transfer
cordova plugin add cordova-plugin-file-opener2
2.下载.apk
var ft = new FileTransfer();
ft.onprogress = function (e) {if (e.lengthComputable) {var progress = e.loaded / e.total * 100;$("#progress").css({ "width": progress + "%" });$("#progress").html(progress + "%");}
}
ft.download(zyd.openApp._downloadUrl, filePath, function (entry) {console.log('下载成功');console.info(JSON.stringify(entry));console.log('文件位置:' + entry.toURL());cordova.plugins.fileOpener2.open(entry.toURL(), 'application/vnd.android.package-archive', {error: function (e) {alert('失败status:' + JSON.stringify(e) + " 路径:" + entry.toURL())},success: function () {alert("安装完成后请重新打开。");}});}, function (err) {console.log("下载失败!");console.info(JSON.stringify(err));
}, null, {});
- vue 使用
downloadApk(url) {const fileTransfer &#61; new FileTransfer()const uri &#61; encodeURI(url)const fileURL &#61; &#39;cdvfile://localhost/temporary/update.apk&#39;fileTransfer.onprogress &#61; (e) &#61;> {if (e.lengthComputable) {this.update.show &#61; truelet progress &#61; Math.ceil(e.loaded / e.total * 100)if (progress < 100) {this.update.progress &#61; progress;} else {this.update.progress &#61; 100;this.update.show &#61; false}}}fileTransfer.download(uri, fileURL,function (entry) {cordova.plugins.fileOpener2.open(entry.toURL(), &#39;application/vnd.android.package-archive&#39;, {error: function (e) {this.$tip("fail", &#96;打开失败&#xff0c;文件路径&#xff1a;${entry.toURL()}&#96;);},success: function () { }})},function (error) {this.$tip("fail", "下载失败");})
}