Can't get Phonegaps FileWriter to work

陌路散爱 提交于 2019-12-12 05:24:54

问题


New to Phonegap and having a hard time understanding the FileWriter function. I am trying to get this example to work on iOS6 but I'm stuck with the error message

    <script type="text/javascript" charset="utf-8" src="cordova-2.3.0.js"></script>
    <script type="text/javascript" charset="utf-8">

        var fileWriter;
        function onNotesLoad() {
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSComplete, fail);
        }
        function onFSComplete(fileSystem) {
            // Load the notes.txt file, create it if it doesn't exist
            fileSystem.root.getFile("notes.txt", {create: true}, onFileEntryComplete, fail);
        }
        function onFileEntryComplete(fileEntry) {
            // set up the fileWriter
            fileEntry.createWriter(onFileWriterComplete, fail);
        }
        function onFileWriterComplete(fileWriter) {
            // store the fileWriter in a
            // global variable so we have it
            // when the user presses save
            fileWriter = fileWriter;
        }
        function saveNotes() {
            // make sure the fileWriter is set
            if (fileWriter != null) {
                // create an oncomplete write function
                // that will redirect the user
                fileWriter.onwrite = function(evt) {
                    alert("Saved successfully");
                    $.mobile.changePage("index.html");
                };
                var form = document.getElementsByTagName('form')[0].elements;
                var notes = form.notes.value;
                // save the notes
                fileWriter.write(notes);
            } else {
                alert("There was an error trying to save the file");
            }
            return false;
        }
        function fail(error) {
            alert(error.code);
        }
        </script>

HTML:

<body>
    <div data-role="page" id="notes-page">
        <div data-role="header" data-position="inline">
            <a href="index.html" data-icon="delete">Cancel</a>
            <h1>Your Thoughts?</h1>
            <a onClick="return saveNotes()" href="#"
                data-icon="check" data-theme="b">Save</a>
        </div>
        <form action="index.html" method="post">
            <textarea name="notes" rows="30" cols="10"></textarea>
        </form>
    </div>
</body>


回答1:


The onwrite handler is called as a progress event. I believe you will want to hook to the onwriteend handler.



来源:https://stackoverflow.com/questions/14329010/cant-get-phonegaps-filewriter-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!