javascript - 在 Cordova/ionic 中写入文件不起作用

标签 javascript file cordova ionic-framework ngcordova

我正在使用 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. 如何读出我刚刚创建的文件?

最佳答案

这个 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);
        }

关于javascript - 在 Cordova/ionic 中写入文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29632077/

相关文章:

javascript - 使用 javascript 从网格中获取所有行,而不仅仅是可见页面上的行

javascript - html表格项中的搜索过滤器[表格数组是异构的]

c++ - 如何在 C++ 中读取文件时向前查看多个步骤

security - 安全客户端-服务器 Phonegap 应用程序的架构

javascript - 将视频从phonegap上传到php

cordova - 为什么 Ionic 社交分享插件不起作用?

javascript - 删除属性 CSS DOM JS

.net - 我应该在.NET应用程序中使用binary/xml文件吗?

java - 为什么我们需要在文件 channel 中使用文件锁,因为操作不是并发访问的?

javascript - copyFileSync不复制文件也不抛出错误