You
You

Reputation: 11

How to integrate file plugin for ionic 2

I was trying to develop a windows 10 application for windows 10 mobiles using Ionic 2, I have a scenario for offline data storage, I tried the sqlite plugin but it was in vain and finally my thoughts end up to use "FILES" to store the data using file plugin sadly this plugin returns me this JavaScript run time error: Unable to get property 'applicationDirectory' of undefined or null reference. Can someone provide me a working example or any suggestions to solve this issue.

Any help will be appreciated.

This is the code snippet that i am working on.

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

import { NavController } from 'ionic-angular';

import { File } from 'ionic-native';

declare var cordova: any;


@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  fs: string = cordova.file.dataDirectory;

  constructor(public navCtrl: NavController) {

    File.checkDir(this.fs, 'mydir').then(_ => console.log('yay')).catch(err => console.log('boooh'));
  }

}

Upvotes: 1

Views: 358

Answers (2)

Enigmatic
Enigmatic

Reputation: 612

This Is my working code of check dir:-

import { File } from 'ionic-native';

var rootPath = cordova.file.externalRootDirectory;
var folderName = 'Your Folder';

this.completePath = rootPath + folderName;
this.subFolder = 'Others';

File.checkDir(this.folderPath + '/', this.subFolder).then((bool) => {
  resolve();
}).catch(() => {
File.createDir(this.folderPath + '/', this.subFolder, false).then(() => {
  resolve();
 })
})

Upvotes: 1

Rohit Gupta
Rohit Gupta

Reputation: 1378

I think the cordova-file-plugin is not loaded or not properly installed.

You should use ionic's wrapper

"ionic plugin add cordova-plugin-file"

You can use Chrome-Developer-Tool to inspect whether the file-plugin has loaded properly or not.

Upvotes: 0

Related Questions