cordova flie文件目录_Cordova文件插件目录错误

因此,我正在用angularjs 1构建一个cordova /

phonegap应用程序,并且试图在该应用程序的专用目录/沙箱中保存和读取一个名为calendar.txt的文件,但不能。

调试时我的控制台日志显示没有错误,如果文件不存在则正在创建文件,并且文件正在正确读取。但是事实并非如此。在设备上构建并运行时,不会保存数据。在指定的位置也没有创建文件。

我在控制台上记录了它试图使用的路径,就是它:file:///data/data/com.adobe.phonegap.app/files/calendar.txt

这是我用来打开文件的代码:

$rootScope.openFile = function(){

var pathToFile = cordova.file.dataDirectory + "calendar.txt";

console.log('path = ' + pathToFile);

window.resolveLocalFileSystemURL(pathToFile,

function(fileEntry){

fileEntry.file(function (file) {

var reader = new FileReader();

reader.onloadend = function (e) {

$rootScope.calendar = JSON.parse(this.result);

console.log('file opened');

console.log(JSON.parse(this.result));

};

reader.readAsText(file);

}, function(error){});

}, function(error){

if(error.code == FileError.NOT_FOUND_ERR){

$rootScope.calendar = new Year();

console.log('no file found so it was created');

$rootScope.saveFile();

}

else{

console.log(error);

}

});

};

这是我保存文件的代码:

$rootScope.saveFile = function(){

var data = JSON.stringify($rootScope.calendar, null, '\t');

var fileName = "calendar.txt"

window.resolveLocalFileSystemURL(cordova.file.dataDirectory,

function(directoryEntry){

directoryEntry.getFile(fileName, { create: true },

function (fileEntry) {

fileEntry.createWriter(

function (fileWriter) {

var blob = new Blob([data], { type: 'text/plain' });

fileWriter.write(blob);

console.log('file saved');

},

function (error){});

},

function (error){}

);

},

function(error){

console.log("Saving Error: Error during finding directory", error.message);

}

);

};

我使用了本教程,以了解更多信息:Cordova文件插件教程

我究竟做错了什么?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值