Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cordova-ionic ngCordova ios or iPhone file read error code 5 ENCODING_ERR

i am using cordova-ionic framework to build app. i am new to the iOS or iPhone in my requirement, i have to read a file in the app. i am reading file in the android app but same code showing error (code: 5).

i am following code types:

in android:

$cordovaFile.writeFile(( 'user.json', data, {'append':false} )).then(function(result) {
  alert('file created.');
    alert(JSON.stringify(result));
}, function(err) {
    // An error occured. Show a message to the user
    alert('file writed');
    alert(JSON.stringify(err));
});

i can create file, writing, reading data and removing the file but in ios phone i am not able to create file using the same code.

in iPhone:

var data = {"user":{"name":"errer","email":"[email protected]","username":"sdfsdfsd"}};

$cordovaFile.writeFile(( 'user.json', data, {'append':false} )).then(function(result) {
    // Success!
    alert('file created.');
    alert(JSON.stringify(result));
}, function(err) {
    // An error occured. Show a message to the user
    alert('file writed');
    alert(JSON.stringify(err));
});

i just change my directory is cordova.file.cacheDirecotry/cordova.file.applicationDirectory

$cordovaFile.createFile(( cordova.file.cacheDirecotry+'user.json', true )).then(function(result) {
    // Success!
    alert('file created.');
    alert(JSON.stringify(result));
}, function(err) {
    // An error occured. Show a message to the user
    alert('file writed');
    alert(JSON.stringify(err));
});

all way getting the error like code: 12 or code: 5

please help me to solve this or give me a idea to get application file path

like image 515
Anil Avatar asked Nov 10 '22 23:11

Anil


1 Answers

I have some progression.

First, I alert my cordova.file.dataDirectory or cordova.file.documentsDirectory. They are

file:///var/mobile/...../Library/NoCloud 

and

file:///var/mobile/..../Documents

Then I create a File without the prefix and succeed. Referring to this https://github.com/driftyco/ng-cordova/issues/362

and the success message shows that the native url of the file is saved in

file:///var/mobile/...../Library/files

Which is quite strange. By the way, I add the

<preference name="iosPersistentFileLocation" value="Library" />

according to https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md#ios-persistent-storage-location

All the tests are running on IOS, i haven't test for Android.

Updates

All the following code worked for me and give success response

$cordovaFile.checkFile('/test.data')

$cordovaFile.createFile('test.data',false)

$cordovaFile.checkDir('/')

Hope this can solve your problems.

like image 153
Windht Avatar answered Nov 14 '22 23:11

Windht