Natanael Natanael - 1 year ago 1569
TypeScript Question

Ionic 2 File Plugin usage examples

Does anyone have complete examples about how to use the Cordova Native File Plugin in a Ionic 2/Angular 2 project?

I installed this plugin but the documentation don't seems to make much sense to me due the fact it is fragmented and lacks of a complete example, including all needed imports.

For example, the following example don't shows where objects like LocalFileSystem or window came from.

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {

console.log('file system open: ' +;
fs.root.getFile("newPersistentFile.txt", { create: true, exclusive: false }, function (fileEntry) {

console.log("fileEntry is file?" + fileEntry.isFile.toString());
// == 'someFile.txt'
// fileEntry.fullPath == '/someFile.txt'
writeFile(fileEntry, null);

}, onErrorCreateFile);

}, onErrorLoadFs);

For example, I need to crate a property file. First I need to check if a file exists on app sandbox storage area, if don't exists I must create it. Then I must open the file write data and save it . How could I do that?

Answer Source

Ionic 2 comes with a Cordova file plugin wrapper:

The necessary file system paths (e.g. cordova.file.applicationDirectory) you can find here at the documentation of the original plugin: Note that not all platforms support the same storage paths.

I even managed to build a file browser with it. Use it like so:

import {Component} from '@angular/core';
import {File} from 'ionic-native';


File.listDir(cordova.file.applicationDirectory, 'mySubFolder/mySubSubFolder').then(
  (files) => {
    // do something
  (err) => {
    // do something
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download