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

I am using a cord-ion structure to create an application. I am new to iOS or iPhone in my requirement, I have to read the file in the application. I am reading a file in an Android application, but the same code shows an error (code: 5).

I follow the types of code:

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 files, write, read data and delete a file, but on an ios phone I cannot create a file using the same code.

on iPhone:

 var data = {"user":{"name":"errer","email":" sdsdff@gmail.com ","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: 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)); }); 

the whole way of getting errors, like code: 12 or code: 5

please help me solve this problem or give me an idea to get the path to the application file.

+5
source share
4 answers

Refer to the source documentation: - https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md#ios-file-system-layout

iOS has some read-only directories. Try changing your path.

Let me know if this does not work for you.

-7
source

I have some progression.

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

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

and

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

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

and a success message indicates that the file’s own URL is stored in

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

This is pretty weird. By the way, I'm adding

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

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

All tests are performed on iOS, I have not tested Android.

Update

All the following code worked for me and gave a successful answer

 $cordovaFile.checkFile('/test.data') $cordovaFile.createFile('test.data',false) $cordovaFile.checkDir('/') 

Hope this can solve your problems.

+3
source

 /* Here is what I am using for my Android and IOS apps Keep attention to a couple of things: - Android and IOS have other directorynames for files - Android devices have different root (myFSRootDirectory1 = Samsung Tab 3, msFSRootDirectory2 = Samsung SII) - $cordovaFile functions prefixes all pathnames with root $cordovaFileTransfer functions needs absolute pathnames Here I create the prefixes for File functions and FileTransfer functions for Android and IOS */ // The $ionicPlatform and ionic.Platorm are from Ionic framework // $ionicPlatform.ready(function() { if (ionic.Platform.isAndroid()) { // If running on Android console.log('cordova.file.externalDataDirectory: ' + cordova.file.externalDataDirectory); // // I use cordova.file.externalDataDirectory because this url is for Android devices // If you remove the app from the device these url are cleared too on the device. So keep it clean. // Remove the root from cordova.file.externalDataDirectory // myFsRootDirectory1 = 'file:///storage/emulated/0/'; // path for tablet myFsRootDirectory2 = 'file:///storage/sdcard0/'; // path for phone fileTransferDir = cordova.file.externalDataDirectory; if (fileTransferDir.indexOf(myFsRootDirectory1) === 0) { fileDir = fileTransferDir.replace(myFsRootDirectory1, ''); } if (fileTransferDir.indexOf(myFsRootDirectory2) === 0) { fileDir = fileTransferDir.replace(myFsRootDirectory2, ''); } console.log('Android FILETRANSFERDIR: ' + fileTransferDir); console.log('Android FILEDIR: ' + fileDir); } if (ionic.Platform.isIOS()) { // if running on IOS console.log('cordova.file.documentsDirectory: ' + cordova.file.documentsDirectory); // I use cordova.file.documentsDirectory because this url is for IOS (NOT backed on iCloud) devices fileTransferDir = cordova.file.documentsDirectory; fileDir = ''; console.log('IOS FILETRANSFERDIR: ' + fileTransferDir); console.log('IOS FILEDIR: ' + fileDir); } if (ionic.Platform.isAndroid() || ionic.Platform.isIOS()) { // // Just functions from the list below one by one ( or chain them) // } }); // Download file from 'http://www.yourdomain.com/test.jpg' to test/one/test.jpg on device Filesystem var hostPath = 'http://www.yourdomain.com/test.jpg'; var clientPath = fileTransferDir + 'test/one/test.jpg'; var fileTransferOptions = {}; $cordovaFile.downloadFile(hostPath, clientPath, true, fileTransferOptions).then (function() { }); // Create dir test $cordovaFile.createDir(fileDir + 'test/').then( function(dirEntry) { }); // Create dir aganin in dir test $cordovaFile.createDir(fileDir + 'test/one/').then( function(dirEntry) { }); // Create empty file test.txt in test/again/ $cordovaFile.createFile(fileDir + 'test/one/test.txt', true).then( function(fileEntry) { }); // List of files in test/again $cordovaFile.listDir(fileDir + 'test/one/').then( function(entries) { console.log('list dir: ', entries); }); // Write some text into file $cordovaFile.writeFile(fileDir + 'test/one/test.txt', 'Some text te test filewrite', '').then( function(result) { }); // Read text written in file $cordovaFile.readAsText(fileDir + 'test/one/test.txt').then( function(result) { console.log('readAsText: ', result); }); 
+3
source

Perhaps this is due to a typo? You have cordova.file.cacheDirecotry . Should not be: cordova.file.cacheDirectory ?

+2
source

Source: https://habr.com/ru/post/1206607/


All Articles