【发布时间】:2015-06-20 08:13:21
【问题描述】:
我正在使用Cordova/ionic 编写应用程序,现在我正在尝试使用$cordovaFile plugin 将文件写入文件系统。因此,我完全尝试了文档中的代码(添加了一些日志记录):
$cordovaFile.writeFile(cordova.file.dataDirectory, "file.txt", "the text inside the file", true)
.then(function (success) {
console.log('SUCCESS: ' + JSON.stringify(success));
}, function (error) {
console.log('ERROR: ' + JSON.stringify(error));
});
但这会返回ERROR: {"code":5},其中5 指的是ENCODING_ERR。
所以在尝试了一些不同的组合之后,将第一行改为这个(所以没有目录):
$cordovaFile.writeFile("file14.txt", "text", true)
返回(为便于阅读而格式化):
SUCCESS: {
"type": "writeend",
"bubbles": false,
"cancelBubble": false,
"cancelable": false,
"lengthComputable": false,
"loaded": 0,
"total": 0,
"target": "fileName": "",
"length": 4,
"localURL": "cdvfile://localhost/persistent/file14.txt",
"position": 4,
"readyState": 2,
"result": null,
"error": null,
"onwritestart": null,
"onprogress": null,
"onwrite": null,
"onabort": null,
"onerror": null
}
所以我尝试使用以下方法读出同一个文件:
$cordovaFile.readAsText("file14.txt")
.then(function (success) {
console.log('SUCCESS: ' + JSON.stringify(success));
}, function (error) {
console.log('ERROR: ' + JSON.stringify(error));
});
令我惊讶的是它只返回一个空字符串:SUCCESS: ""
所以我现在想知道:
- 为什么复制粘贴示例代码会导致
5 ENCODING_ERR? - 为什么我删除目录后它会起作用?
- 如何读出我刚刚创建的文件?
【问题讨论】:
-
为什么不使用cordova原生文件系统?
-
@ProllyGeek - 我不确定你的意思。我的插件是这个:github.com/apache/cordova-plugin-file,我找不到其他的。有什么建议吗?
-
@karmer65 是的,我的意思是最新版本是 1.3.3,你为什么使用成功函数作为对象,我认为你对成功数据和成功函数感到困惑。
-
我遇到了类似的错误。我在路径
..\Phone\Android\data\com.ionicframework.appname761153\files中创建了一个名为 test.txt 的文件。它成功,正如我在调试中看到的那样(通过 USB 直接从 Android 设备),但文件夹中没有这样的文件。
标签: javascript file cordova ionic-framework ngcordova