Cordova-plugin-file File writer help

Hi All,

I’ve been trying for a number of hours spread over a number of days to get a file writer going based on the cordova-plugin-file plugin as a part of ngCordova,

Where I am struggling is in getting the directory right ( I assume ). I have the project working fine, and I can read files fine; but what I can’t do is write a file with data I get either error code 5 (ENCODING_ERR) or error code 10 (QUOTA_EXCEEDED_ERR). I have tried my best to follow the guide, but I can only guess I’m missing something in a config file or doing something you shouldn’t. Here is one of my attempts:

if(ionic.Platform.is("Android"))
                {
                    directory = cordova.file.applicationStorageDirectory;
                }

                $cordovaFile.createDir(directory);
                directory = directory +"filename.txt";
                console.log(directory);


                $cordovaFile.createFile(directory, true),

                $cordovaFile.writeFile(directory, data, {'append':false} ).then( function(result) {
                    // Success!
                    console.log("$ngCordovaFile writeFile SUCCESS");
                    console.log(result);
                }, function(err) {
                    // An error occured. Show a message to the user
                    console.log("$ngCordovaFile writeFile ERROR");
                    console.log(err);
                });

Can anyone point me in the right direction?

I wrote a topic about cordova file

http://forum.ionicframework.com/t/how-to-write-data-to-a-file-using-ngcordova/10943/7?u=pcr

hi pcr,

Thanks for the link - I have been using your post since I started looking at this requirement - nice article.

I’ve given your code a shot and get null exception for cordova.file.externalDataDirectory for if (fileTransferDir.indexOf is this just the directory not being there on my lg g2 ? Is there an internal directory you could recommend to use, as I am aware that a lot of androids don’t have an external SD

I have tried a number of the internal directories but I get either an error code 5 or 10? Perhaps this is just the phone I’m using, but before spending a couple of hours investigating, just thought to ask first.

Cheers

Hi. Are you sure you run this on the mobile device and not in a desktop browser. If in desktop browser cordiva is not present, so cordova.file… Is not there. This only work after install your app on a mobile device.
I don’t think your LG can’t handle this. Maybe you try cordova.file.dataDirectory. This is the usual dir for all internal memory.

Hi, Yep all of my testing and R&D for this project is on the device as I am using a sqlite db. Found out that if I don’t specify a directory the file saves happily in the /storage/emulated/0/ directory. I was hoping to use one of the other directories, but this for now is enough for me. If you have any recommendations on the ideal location to put files let me know, but all I can assume for now is that certain directories which I should be able to write to are locked down in someway by LG.

On the plus side I can now save a file, the work you put in pointed me in the right direction so again thank you!

John

Hi. That’s OK. If you don’t give a dir. So it seems to work. To use a dir you use: fileTransferDir + 'test/test.txt. FileTrnsfer creates the dir automatically.

If you want read the file after transfer you use: fileDir + 'test/test.txt

About the best dir to put files on:

Again use my computed dir. If you use externalDataDir you will see that the file goes to:
storage/emulated/0/Android/appname/files/
This is a dir for specific your app. Other apps can’t read nor write in this dir. This dir is persistent. Some other dirs are not. Because if Android needs memory if you run other apps and run short Android decides to removes not persistant dirs. So cordova.file.dataDirectory or cordova.file.externalDataDirectory is the best you can use.
For IOS the best dir is: cordova.file.documentsDirectory. Beside of persistant this dir is not automatically backupped into the IOS iCloud.

2 Likes

got all up and running with android, but I’m finding I get a Error 5 dir not found when using cordova.file.documentsDirectory within the simulator - is this expected, will the cordova.file.documentsDirectory work on the actual device only?

Yes. ngCordova is ment to be working for mobile devices. This functions dont work in a desktop-browser.

BTW
documentsDirectory is specific IOS. So in your app you need to determine is your app running is on IOS with:

if (ionic.Platform.isIOS()) {

for Android device you need to use: cordova.file.dataDirectory or cordova.file.externalDataDirectory with:

if (ionic.Platform.isAndroid()) {

The reason why:

cordova.file.applicationStorageDirectory

didnt work is that this dir is read-only!!!

success

Does console.log(JSON.stringify(cordova.file)) returns following output on iOS simulator ?

{ "applicationDirectory":null, "applicationStorageDirectory":null, "dataDirectory":null, "cacheDirectory":null, "externalApplicationStorageDirectory":null, "externalDataDirectory":null, "externalCacheDirectory":null, "externalRootDirectory":null, "tempDirectory":null, "syncedDataDirectory":null, "documentsDirectory":null, "sharedDirectory":null }

Hey @pcr

Do you know if there is any directory where external app can write into? (my goal: create a txt file and open it with WORD)

Thx!

Geoffrey