显示使用cordova文件传输下载的百分比

时间:2017-08-13 18:05:41

标签: cordova ionic-framework ionic2

我可以使用以下脚本将文件下载到设备。而我想知道的是能够显示在视图中下载的百分比。

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();

      });

    });

  }

2 个答案:

答案 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);
});