cordova.fileTransfer not updating page template Ionic2+Cordova

raju

I have created an Ionic2 App using cordova FileTransferplugin, i am downloading remote server file.

Everything is working perfectly, but when I try to update template while fileTransfer.OnProgress event, the template is not updating the % downloaded.

Pleas see this video for my problem. Ionic_youtube_link

My Code is, logic is in downloadFile function

import {Component, AfterViewInit, ViewChild} from '@angular/core';
import {NavController, NavParams, ViewController, Nav} from 'ionic-angular';

import {Page, Platform} from 'ionic-angular';
import {File, Device, Cordova, Transfer} from 'ionic-native';

import { SafeResourceUrl, DomSanitizationService } from '@angular/platform-browser';

@Component({
  templateUrl: 'build/pages/video-download-modal/video-download-modal.html',
  providers: [File, Transfer]
})
export class VideoDownloadModal {
  selectedItem: any;
  @ViewChild(Nav) nav: Nav;

  videoPathUrl: string;
  isPlatformReady: boolean;
  platformName: string;
  directoryName: string = "socialAppDir";

  totalVideoSize:number;
  totalDownloaded:number;
  totalPercent:string = "0%";

  constructor(public navCtrl: NavController, navParams: NavParams, private _viewController: ViewController, platform: Platform, private transfer:Transfer) {
    // If we navigated to this page, we will have an item available as a nav param
    if (platform.is('core')) {//if on dekstop
      console.log('dektop');
    } else {
      this.videoPathUrl = navParams.get('videoPath');
      console.log(this.videoPathUrl);
      platform.ready().then((readySource) => {
        this.isPlatformReady = true;
        console.log('ready 1234');
        const fs: string = cordova.file.externalRootDirectory;
        console.log(cordova.file.dataDirectory);
        this.platformName = Device.device.platform;

        File.checkDir(cordova.file.externalDataDirectory, this.directoryName).then(() => {
          console.log('directory exists');
          this.downloadFile();
        }, (error) => {
          console.log('directory not exists');
          this.createDirectory();
        })

      })
    }
  }
  dismiss() {
    let data = { 'foo': 'bar' };
    this._viewController.dismiss(data);
  }

  createDirectory():void{
    File.createDir(cordova.file.externalDataDirectory, this.directoryName, true).then(() => {
      console.log("created externalDataDirectory");
      this.downloadFile();
    },(error) => {
      console.log('some error happen')
    })
  }

  downloadFile = () => {
    console.log(this);
    let fileName: string = this.videoPathUrl.split("/").pop();
    let targetPath = cordova.file.externalDataDirectory + this.directoryName + "/" + fileName;
    console.log(targetPath);
    this.transfer.download(this.videoPathUrl, targetPath, true, {}).then(() => {
      console.log('video downloaded')
    }, (error) => {
      console.log(error)
    })
    this.transfer.onProgress((progress) => {
      console.log(this);
      this.totalVideoSize = progress.total;
      this.totalDownloaded = progress.loaded;
      this.totalPercent = ((progress.loaded / progress.total) * 100).toString();

      console.log(this.totalPercent);
    })
  }

  ionViewDidEnter() {
    console.log("enter login1");
  }    
}

And HTML is

<ion-content>


<div id="modalContainer" class="abd">
    <ion-spinner></ion-spinner>
    <br />
    {{**totalPercent**}}
    <br />
    <button dnager block (click)="dismiss()">Exit</button>
  </div>
</ion-content>

The totalPercent value either has 0 or 100.But not updating. Please help.

Ken Chang

This is because the totalPercent of this inside the handler was set to the global Window object instead of the object itself.

I have finally got it to work

import { NgZone } from '@angular/core';

fileTransfer.onProgress((progressEvent: ProgressEvent) => {
  this.ngZone.run(() => {
    if (progressEvent.lengthComputable) {
      let lp = progressEvent.loaded / progressEvent.total * 100;
      this.loadingPercent = Math.round(lp * 100) / 100;
    }
  });
})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unable to download file in Cordova and Ionic with FileTransfer

From Dev

Cordova FileTransfer Download error

From Dev

FileTransfer Cordova download path

From Dev

FileTransfer Cordova download path

From Dev

Use Cordova or Ionic2?

From Dev

Cordova Android Filetransfer returning null

From Dev

Download file via FileTransfer in Cordova

From Dev

Cordova FileTransfer show downloaded image

From Dev

Cordova/Phonegap does not recognise FileTransfer

From Dev

Download file via FileTransfer in Cordova

From Dev

Cordova FileTransfer not sending custom headers

From Dev

Cordova: Uncaught ReferenceError: FileTransfer is not defined

From Dev

Unable to upload file with Cordova FileTransfer

From Dev

Ionic2 Cordova Camera Not Working

From Dev

Cordova's FileTransfer Writing Error (Code 1)

From Dev

Cordova FileTransfer plugin params empty on Android

From Dev

Save file to public directory using Cordova FileTransfer

From Dev

Cordova FileTransfer Download - always returns error 3

From Dev

Updating a Ionic framework project to the latest cordova

From Dev

Ionic: Getting Error Code 3 when uploading image using ng-cordova fileTransfer and Camera plugin

From Dev

Angular2 Ionic2 create plugin with cordova

From Dev

Apache Cordova Application Not Updating

From Dev

Access Cordova plugins directly from ionic2 typescript components

From Dev

Cordova-plugin-printer crashing Ionic2 app

From Dev

Cannot assign Cordova plugin returned value to views in Ionic2

From Dev

Error while running Cordova build android ionic2

From Dev

Cordova (Ionic2) custom plugin: manage Angular2 new Promise() with cordova.exec() successHandler

From Dev

Template variable not updating after scrollEvent in ionic2

From Dev

Cordova fileTransfer works perfect on iOS, throws error code = 1 on Android

Related Related

  1. 1

    Unable to download file in Cordova and Ionic with FileTransfer

  2. 2

    Cordova FileTransfer Download error

  3. 3

    FileTransfer Cordova download path

  4. 4

    FileTransfer Cordova download path

  5. 5

    Use Cordova or Ionic2?

  6. 6

    Cordova Android Filetransfer returning null

  7. 7

    Download file via FileTransfer in Cordova

  8. 8

    Cordova FileTransfer show downloaded image

  9. 9

    Cordova/Phonegap does not recognise FileTransfer

  10. 10

    Download file via FileTransfer in Cordova

  11. 11

    Cordova FileTransfer not sending custom headers

  12. 12

    Cordova: Uncaught ReferenceError: FileTransfer is not defined

  13. 13

    Unable to upload file with Cordova FileTransfer

  14. 14

    Ionic2 Cordova Camera Not Working

  15. 15

    Cordova's FileTransfer Writing Error (Code 1)

  16. 16

    Cordova FileTransfer plugin params empty on Android

  17. 17

    Save file to public directory using Cordova FileTransfer

  18. 18

    Cordova FileTransfer Download - always returns error 3

  19. 19

    Updating a Ionic framework project to the latest cordova

  20. 20

    Ionic: Getting Error Code 3 when uploading image using ng-cordova fileTransfer and Camera plugin

  21. 21

    Angular2 Ionic2 create plugin with cordova

  22. 22

    Apache Cordova Application Not Updating

  23. 23

    Access Cordova plugins directly from ionic2 typescript components

  24. 24

    Cordova-plugin-printer crashing Ionic2 app

  25. 25

    Cannot assign Cordova plugin returned value to views in Ionic2

  26. 26

    Error while running Cordova build android ionic2

  27. 27

    Cordova (Ionic2) custom plugin: manage Angular2 new Promise() with cordova.exec() successHandler

  28. 28

    Template variable not updating after scrollEvent in ionic2

  29. 29

    Cordova fileTransfer works perfect on iOS, throws error code = 1 on Android

HotTag

Archive