Hi,
I am trying to use cordova.file.dataDirectory constant but it doesnt seem to work
I am importing the File class from ionic-native.
Is there something I have missed?
thanks
Hi,
I am trying to use cordova.file.dataDirectory constant but it doesnt seem to work
I am importing the File class from ionic-native.
Is there something I have missed?
thanks
at the moment you have missed posting your code. Can’t help unless you show us whats going on? I use cordova.file.dataDirectory in my Ionic app and works just fine.
Hi,
Sorry for not being clear.
My code is simply:
import {File, Cordova} from ‘ionic-native’;
export class HomePage {
doCreateDir() {
File.createDir(Cordova.file.dataDirectory, “MyDirName”, true);
}
}
I am getting a typescript error:
[ts]
Property ‘file’ does not exist on type ‘(opts?: any) => (target: Object, methodName: string, descriptor: TypedPropertyDescriptor) =>…’.
“file” (in Cordova.file.dataDirectory) is underlined by a red squiggly line in my Visual Studio Code IDE.
Thanks
its supposed to be lower case “cordova.file.dataDirectory” …not “Cordova.file…”
if that doesn’t solve the issue, just use the documentation from the cordova-plugin-file page to do a barebones file creation just to see/get it working. Then go back and analyze the differences.
This is my use of it, pretty standard:
function writeToFile(fileName, data) {
data = JSON.stringify(data, null, '\t');
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (directoryEntry) {
directoryEntry.getFile(fileName, { create: true }, function (fileEntry) {
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function (e) {
errMgmt("index/writeToFile",1152,fileName +" - successfully written") ;
};
fileWriter.onerror = function (e) {
errMgmt("index/writeToFile",1153,fileName+ " - failed to write") ;
};
var blob = new Blob([data], { type: 'text/json' });
fileWriter.write(blob);
}, errorHandler.bind(null, fileName, 1154));
}, errorHandler.bind(null, fileName,1155));
}, errorHandler.bind(null, fileName,1156));
}
cordova.file.dataDirectory doesn’t work either.
What classes are you importing and from which libraries?
Are you using ionic 2?
I am reading the documentation from:
And can find ant y mention of cordova.file.dataDirectory
Many Thanks for your help.
no, I am on Ionic 1.7 still All my documentation comes directly from the cordova-file-plugin (which is where cordova.file.dataDirectory originates).
The question is for Ionic 2.
I haven’t used Ionic 1 before but I believe ionic 2 is an entirely different framework rebuilt so your answer isn’t any good.
Probably the easiest way to deal with this is:
declare var cordova:any;
It’s a bit kludgy, but this is one of those areas where TypeScript’s compatibility with JavaScript makes for ugliness.
Hi,
I couldn’t seem to get this to work.
I added declare var cordova:any; to the top of my typescript file and my app can compile.
In my button click handler code, I put:
…
if (cordova != null) {
if (cordova.file != null) {
if (cordova.file.dataDirectory != null) {
alert(cordova.file.dataDirectory);
} else {
alert(“no cordova.file.dataDirectory”);
}
} else {
alert(“no cordova.file”);
}
} else {
alert(“cordova undefined”);
}
…
The result I got after clicking my button was “no cordova.file” .
I am running this on my iphone using Ionic View app.
Any ideas will be appreciated.
Thanks
Run in the same problem, not sure why the declare isn’t sufficient. But you can always use a try / catch-statement as a workaround.
try {
return cordova.file.dataDirectory;
} catch(err) {
console.log("no cordova.file.dataDirectory");
}