3

请帮忙,我正在使用 phoneGap 3.0.0 测试一个简单的写入/读取文件示例(在 IOS 上测试)

写入有效,我可以看到数据正确写入文件系统上的文件中,但由于某种原因,尽管总是返回 onloadend,但读取总是返回 NULL 结果?

任何想法我做错了什么?

这是代码。

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width;" />
    <title>Open File</title>
    <script type="text/javascript" src="js/xui-2.3.2.js"></script>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" >

    var fileObject;

    document.addEventListener("deviceready", onDeviceReady, true);

    function onDeviceReady() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail); 
    }

    function onFileSystemSuccess(fileSystem) {
        fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
    }

    function gotFileEntry(fileEntry) {
        fileObject = fileEntry;

        x$('#saveFile_btn').on('click', function() {
            saveFileContent();
        });
    }

    function gotFileWriter(writer) {
        var myText = document.getElementById('my_text').value;
        /* prepare write listeners */
        writer.onwriteend = function(evt) {
            console.log("DONE Writing text");
            x$('#message').html('<p>File contents have been written.<br /><strong>File path:</strong> ' + fileObject.fullPath + '</p>');

            var reader = new FileReader();
            /* prepare read listeners */
            reader.onloadstart = function(evt) {
                console.log("started reading");
            };
            reader.onabort = function(evt) {
                console.log("aborted read text");
                console.log(evt.target.result);
                x$('#contents').html('<strong>Aborted</strong> <br />');
            };
            reader.onerror = function(evt) {
                console.log("Error read text");
                console.log("Error"+evt.error.code);
                x$('#contents').html('<strong>Error:</strong> <br />' + evt.error.code);
            };
            reader.onloadend = function(evt) {
                console.log("successfully read text");
                console.log("Target Result ["+evt.target.result+"]");
                console.log("Reader Result ["+reader.result+"]");
                x$('#contents').html('<strong>File contents:</strong> <br />' + evt.target.result);
            };

            console.log("Reading text");
            reader.readAsText(fileObject);
        };
        console.log("Writing text ["+myText+"]");
        writer.write(myText);

    }

    function saveFileContent() {
        fileObject.createWriter(gotFileWriter, fail);
    }

    function fail(error) {
        alert(error.code);
    }
    </script>
</head>
<body>

    <input type="text" id="my_text" />
    <input type="button" id="saveFile_btn" value="Save" />

    <div id="message"></div>
    <div id="contents"></div>

</body>
</html>

这是控制台输出。

2013-08-22 13:55:22.215 Chapter2[33715:c07]  Writing text [sdsd]
2013-08-22 13:55:22.219 Chapter2[33715:c07]  DONE Writing text
2013-08-22 13:55:22.219 Chapter2[33715:c07]  Reading text
2013-08-22 13:55:22.220 Chapter2[33715:c07]  started reading
2013-08-22 13:55:22.221 Chapter2[33715:c07]  successfully read text
2013-08-22 13:55:22.222 Chapter2[33715:c07]  Target Result []
2013-08-22 13:55:22.222 Chapter2[33715:c07]  Reader Result []
4

1 回答 1

2

感谢 Raymond Camden 帮助我解决了这个问题。在 phonegap 3.0.0 中更改为文件 API

这是工作版本

传递给 readFile 的对象是一个 FileEntry 对象。为了使用 FileEntry 的实际文件,您可以使用 file 方法

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width;" />
    <title>Open File</title>
    <script type="text/javascript" src="js/xui-2.3.2.js"></script>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" >

    var fileObject;

    document.addEventListener("deviceready", onDeviceReady, true);

    function onDeviceReady() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail); 
    }

    function onFileSystemSuccess(fileSystem) {
        fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
    }

    function gotFileEntry(fileEntry) {
        fileObject = fileEntry;

        x$('#saveFile_btn').on('click', function() {
            saveFileContent();
        });
    }

    function gotFileWriter(writer) {
        var myText = document.getElementById('my_text').value;
        /* prepare write listeners */
        writer.onwriteend = function(evt) {
            console.log("DONE Writing text");
            x$('#message').html('<p>File contents have been written.<br /><strong>File path:</strong> ' + fileObject.fullPath + '</p>');

            fileObject.file(function(e) {

                console.log("called the file func on the file ob");

                var reader = new FileReader();
                /* prepare read listeners */
                reader.onloadstart = function(evt) {
                    console.log("started reading");
                };
                reader.onabort = function(evt) {
                    console.log("aborted read text");
                    console.log(evt.target.result);
                    x$('#contents').html('<strong>Aborted</strong> <br />');
                };
                reader.onerror = function(evt) {
                    console.log("Error read text");
                    console.log("Error"+evt.error.code);
                    x$('#contents').html('<strong>Error:</strong> <br />' + evt.error.code);
                };
                reader.onloadend = function(evt) {
                    console.log("successfully read text");
                    console.log("Target Result ["+evt.target.result+"]");
                    console.log("Reader Result ["+reader.result+"]");
                    x$('#contents').html('<strong>File contents:</strong> <br />' + evt.target.result);
                };

                console.log("Reading text");
                reader.readAsText(e);
            });
        };
        console.log("Writing text ["+myText+"]");
        writer.write(myText);

    }

    function saveFileContent() {
        fileObject.createWriter(gotFileWriter, fail);
    }

    function fail(error) {
        alert(error.code);
    }
    </script>
</head>
<body>

    <input type="text" id="my_text" />
    <input type="button" id="saveFile_btn" value="Save" />

    <div id="message"></div>
    <div id="contents"></div>

</body>
</html>
于 2013-08-27T14:41:23.130 回答