7

import {Component} from '@angular/core';
import {NavController, Platform, AlertController} from 'ionic-angular';
import {Transfer, TransferObject} from '@ionic-native/transfer';
import {File} from '@ionic-native/file';



@Component({
  selector: 'page-about',
  templateUrl: 'about.html',
  providers: [Transfer, TransferObject, File]
})
export class AboutPage {

  storageDirectory: string = '';

  constructor(public navCtrl: NavController, public platform: Platform, private transfer: Transfer, private file: File, public alertCtrl: AlertController) {
    this.platform.ready().then(() => {
      // make sure this is on a device, not an emulation (e.g. chrome tools device mode)
      if(!this.platform.is('cordova')) {
        return false;
      }

      if (this.platform.is('ios')) {
        this.storageDirectory = cordova.file.documentsDirectory;
      }
      else if(this.platform.is('android')) {
        this.storageDirectory = cordova.file.externalDataDirectory;
		console.log(this.storageDirectory);
      }
      else {
        // exit otherwise, but you could add further types here e.g. Windows
        return false;
      }
    });
  }
  
   downloadImage() {

    this.platform.ready().then(() => {

      const fileTransfer: TransferObject = this.transfer.create();

      const imageLocation = 'http://html5demos.com/assets/dizzy.mp4';

      fileTransfer.download(imageLocation, this.storageDirectory + 'dizzy.mp4').then((entry) => {
       
	   const alertSuccess = this.alertCtrl.create({
          title: `Download Succeeded!`,
          subTitle: `successfully downloaded to: ${entry.toURL()}`,
          buttons: ['Ok']
        });

        alertSuccess.present();

      }, (error) => {

        const alertFailure = this.alertCtrl.create({
          title: `Download Failed!`,
          subTitle: `was not downloaded. Error code: ${error}`,
          buttons: ['Ok']
        });

        alertFailure.present();

      });

    });

  }


}

I am getting the error attached in screenshot.I am getting error while running my project build in ionic 2, though i have installed 'typings' with below command

npm install -g typings typings, install dt~cordova --save --global

and tried every possible method to remove this error, checked all cordova plugin like File, file transfer but still error is not resolving.

Can anyone look for it.

Here attached also the code, i dont have any idea where i am going wrong..

1

2 Answers 2

27

I have edited your code adding the declare let cordova: any; This exposes the cordova api for use.Hope this helps.

import {Component} from '@angular/core';
import {NavController, Platform, AlertController} from 'ionic-angular';
import {Transfer, TransferObject} from '@ionic-native/transfer';
import {File} from '@ionic-native/file';

declare let cordova: any;

@Component({
  selector: 'page-about',
  templateUrl: 'about.html',
  providers: [Transfer, TransferObject, File]
})
export class AboutPage {

  storageDirectory: string = '';

  constructor(public navCtrl: NavController, public platform: Platform, private transfer: Transfer, private file: File, public alertCtrl: AlertController) {
    this.platform.ready().then(() => {
      // make sure this is on a device, not an emulation (e.g. chrome tools device mode)
      if(!this.platform.is('cordova')) {
        return false;
      }

      if (this.platform.is('ios')) {
        this.storageDirectory = cordova.file.documentsDirectory;
      }
      else if(this.platform.is('android')) {
        this.storageDirectory = cordova.file.externalDataDirectory;
		console.log(this.storageDirectory);
      }
      else {
        // exit otherwise, but you could add further types here e.g. Windows
        return false;
      }
    });
  }
  
   downloadImage() {

    this.platform.ready().then(() => {

      const fileTransfer: TransferObject = this.transfer.create();

      const imageLocation = 'http://html5demos.com/assets/dizzy.mp4';

      fileTransfer.download(imageLocation, this.storageDirectory + 'dizzy.mp4').then((entry) => {
       
	   const alertSuccess = this.alertCtrl.create({
          title: `Download Succeeded!`,
          subTitle: `successfully downloaded to: ${entry.toURL()}`,
          buttons: ['Ok']
        });

        alertSuccess.present();

      }, (error) => {

        const alertFailure = this.alertCtrl.create({
          title: `Download Failed!`,
          subTitle: `was not downloaded. Error code: ${error}`,
          buttons: ['Ok']
        });

        alertFailure.present();

      });

    });

  }


}

Sign up to request clarification or add additional context in comments.

Comments

0

You have to declare cordova namespace in src/declarations.d.ts file so the typescript transpiler can understand that cordova is declared and referred to Object, but in your case if you want to use a plugin, its better to use ionic-native and if the plugin is not listed there, declare the namespace of the plugin and use it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.