Javascript 在Cordova/ionic中写入文件';行不通
我正在使用/编写一个应用程序,现在我正在尝试使用将文件写入文件系统。因此,我尝试了文档中的代码(添加了一些日志): 但这将返回Javascript 在Cordova/ionic中写入文件';行不通,javascript,file,cordova,ionic-framework,ngcordova,Javascript,File,Cordova,Ionic Framework,Ngcordova,我正在使用/编写一个应用程序,现在我正在尝试使用将文件写入文件系统。因此,我尝试了文档中的代码(添加了一些日志): 但这将返回错误:{“code”:5},其中5指的是编码错误 因此,在尝试了一些不同的组合后,将第一行更改为(因此不使用dir): 返回(为可读性而格式化): 因此,我尝试使用以下方法读取相同的文件: $cordovaFile.readAsText("file14.txt") .then(function (success) { console.log('SU
错误:{“code”:5}
,其中5
指的是编码错误
因此,在尝试了一些不同的组合后,将第一行更改为(因此不使用dir):
返回(为可读性而格式化):
因此,我尝试使用以下方法读取相同的文件:
$cordovaFile.readAsText("file14.txt")
.then(function (success) {
console.log('SUCCESS: ' + JSON.stringify(success));
}, function (error) {
console.log('ERROR: ' + JSON.stringify(error));
});
令我惊讶的是,它只返回一个空字符串:SUCCESS:
所以我现在想知道:
为什么复制粘贴示例代码会导致5编码错误
当我删除dir时,它为什么工作
如何读取刚刚创建的文件
这个很容易理解,它在英特尔xdk上工作,但它使用cordova,所以只要您有cordova(它与英特尔xdk api无关),就可以工作,只要确保代码中定义了cordova.file
对象即可
请注意,cordova文件系统从1.2版开始有了巨大的变化,同时请注意,当前的1.3.3版在从当前工作区(www文件夹)加载文件方面存在一些错误,但是在应用程序存储文件夹或内部存储中写入和读取文件没有任何问题
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady()
{
requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, onError);
}
function onSuccess(fileSystem)
{
var directoryEntry = fileSystem.root;
//lets create a file named readme.txt. getFile method actually creates a file and returns a pointer(FileEntry) if it doesn't exist otherwise just returns a pointer to it. It returns the file pointer as callback parameter.
directoryEntry.getFile("readme.txt", {create: true, exclusive: false}, function(fileEntry){
//lets write something into the file
fileEntry.createWriter(function(writer){
writer.write("This is the text inside readme file");
}, function(error){
console.log("Error occurred while writing to file. Error code is: " + error.code);
});
}, function(error){
console.log("Error occurred while getting a pointer to file. Error code is: " + error.code);
});
}
function onError(evt)
{
console.log("Error occurred during request to file system pointer. Error code is: " + evt.code);
}
为什么不使用cordova本机文件系统?@ProllyGeek-我不知道你的意思。我使用的插件是这个:我找不到其他插件。有什么建议吗?@karmer65是的,这是我的意思,最新版本是1.3.3,为什么你要用success函数作为对象,我想你把success数据和success函数混淆了。我有一个类似的错误。我在路径。\Phone\Android\data\com.ionicframework.appname761153\files
中创建了一个名为test.txt的文件。正如我在调试中看到的那样,它成功了(直接从Android设备通过USB),但文件夹中没有这样的文件。
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));
});
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady()
{
requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, onError);
}
function onSuccess(fileSystem)
{
var directoryEntry = fileSystem.root;
//lets create a file named readme.txt. getFile method actually creates a file and returns a pointer(FileEntry) if it doesn't exist otherwise just returns a pointer to it. It returns the file pointer as callback parameter.
directoryEntry.getFile("readme.txt", {create: true, exclusive: false}, function(fileEntry){
//lets write something into the file
fileEntry.createWriter(function(writer){
writer.write("This is the text inside readme file");
}, function(error){
console.log("Error occurred while writing to file. Error code is: " + error.code);
});
}, function(error){
console.log("Error occurred while getting a pointer to file. Error code is: " + error.code);
});
}
function onError(evt)
{
console.log("Error occurred during request to file system pointer. Error code is: " + evt.code);
}