【问题标题】:Writing files in Cordova/ionic doesn't work在 Cordova/ionic 中写入文件不起作用
【发布时间】: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: ""

所以我现在想知道:

  1. 为什么复制粘贴示例代码会导致5 ENCODING_ERR
  2. 为什么我删除目录后它会起作用?
  3. 如何读出我刚刚创建的文件?

【问题讨论】:

  • 为什么不使用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


【解决方案1】:

这个tutorial很容易理解,它适用于intel xdk,但它使用cordova,所以只要你有cordova就可以工作(它与intel xdk api无关) ,只需确保在您的代码中定义了 cordova.file 对象。

请注意,从 1.2 版开始,cordova 文件系统发生了巨大变化,还请注意,当前 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);
        }

【讨论】:

    猜你喜欢
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    相关资源
    最近更新 更多