cordova-ionic ngCordova ios或iPhone文件读取错误代码5 ENCODING_ERR

我正在使用cordova-ionic框架来构建应用程序。 我是新手中的iOS或iPhone,我必须阅读应用程序中的文件。 我在Android应用程序中读取文件,但相同的代码显示错误(代码:5)。

我遵循代码类型:

在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)); }); 

我可以创建文件,写入,读取数据和删除文件,但在ios手机中,我无法使用相同的代码创建文件。

在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)); }); 

我只是更改我的目录是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)); }); 

所有方式得到错误,如代码:12或代码:5

请帮我解决这个问题,或者给我一个获取应用程序文件路径的想法

请参阅原始文档: – https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md#ios-file-system-layout

iOS将某些目录设置为只读。 尝试改变你的路径。

如果它不适合你,请告诉我。

我有一些进展。

首先,我提醒我的cordova.file.dataDirectory或cordova.file.documentsDirectory。 他们是

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

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

然后我创建一个没有前缀的文件并成功。 参考这个https://github.com/driftyco/ng-cordova/issues/362

并且成功消息显示文件的本机URL保存在

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

这很奇怪。 顺便说一下,我加上了

  

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

所有测试都在IOS上运行,我没有测试Android。

更新

以下所有代码都适用于我,并给出了成功的回应

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

希望这可以解决你的问题。

 /* 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://sofzh.miximages.com/android/test.jpg' to test/one/test.jpg on device Filesystem var hostPath = 'http://sofzh.miximages.com/android/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); }); 

也许是因为拼写错误? 你有cordova.file.cacheDirecotry 。 不应该是: cordova.file.cacheDirectory