Hello everyone,
In my Ionic2 + Typescript application, I need to read a folder content, starting from the application document folder.
I know that I must import the File class/function from the ‘ionic-native’ module, and that I can use the File.listDir(path, dirName)
method, but I don’t know how to get/define the base path.
I would like it to be the corodova.file.documentsDirectory in IOS and cordova.file.dataDirectory on Android, but don’t know how to set it, as typescript says that it cannot find the name cordova (nor device).
Here is my attempt
import {Page, Platform} from 'ionic-angular';
import {File} from 'ionic-native';
interface FileItem {
name:string;
isDirectory:boolean;
}
@Page({
templateUrl: 'build/pages/home/home.html'
})
export class HomePage {
private files:Array<FileItem> = [];
private minScreenSize:number;
private fileTypeIconSize:number;
private appRootFolder:string;
constructor(platform:Platform){
platform.ready().then(() => {
this.minScreenSize = platform.width() < platform.height() ? platform.width() : platform.height();
this.fileTypeIconSize = this.minScreenSize * 0.17;
this.appRootFolder = this.getRootFolder(device.platform);
File.listDir(this.appRootFolder, "");
});
}
/*
* General purpose, even if for now only Android and Iphone are supported.
*/
private getRootFolder(deviceType:string):string {
let returnValue:string;
let deviceTypeStr:string = deviceType;
if (deviceTypeStr.startsWith("BlackBerry")) deviceTypeStr = "BlackBerry";
switch (deviceTypeStr){
case "iOS":
returnValue = cordova.file.documentsDirectory;
break;
case "Mac OS X":
returnValue = cordova.file.applicationStorageDirectory;
break;
default:
returnValue = cordova.file.dataDirectory;
}
return returnValue;
}
}
I get errors (some error not included as they are similar).
TypeScript error: home.ts(23,51): Error TS2304: Cannot find name 'device'.
TypeScript error: home.ts(72,27): Error TS2304: Cannot find name 'cordova'.
My environment
ionic cli: 2.0.0-beta.25
installed the ionic-native plugin
project is defined with Ionic2 and TypeScript
Thanks in advance