我可以使用以下脚本将文件下载到设备。而我想知道的是能够显示在视图中下载的百分比。
downloadImage() {
this.platform.ready().then(() => {
const fileTransfer: FileTransferObject = this.Transfer.create();
const audiolocation = `http://myweb.com/files`+this.audio_download;
fileTransfer.download(audiolocation, this.storageDirectory+'downloads').then((entry) => {
const alertSuccess = this.alertCtrl.create({
title: `Download Succeeded!`,
subTitle: `Audio was successfully downloaded to: ${entry.toURL()}`,
buttons: ['Ok']
});
alertSuccess.present();
}, (error) => {
const alertFailure = this.alertCtrl.create({
title: `Download Failed!`,
subTitle: `was not successfully downloaded. Error code: ${error.code}`,
buttons: ['Ok']
});
alertFailure.present();
});
});
}
答案 0 :(得分:0)
文件传输具有onProgress方法。请参阅此link
var fileTransfer= new FileTransfer();
fileTransfer.onprogress = function(progressEvent) {
var percent = progressEvent.loaded / progressEvent.total * 100;
percent = Math.round(percent);
console.log(percent);
};
//fileTransfer.download(...); // or fileTransfer.upload(...);
希望这有帮助。
答案 1 :(得分:0)
更像这样:
fileTransfer.onProgress((progressEvent) => {
var percent = progressEvent.loaded / progressEvent.total * 100;
this.percentage = Math.round(percent);
console.log(this.percentage);
});