0

我正在尝试使用 phonegap filewriter API 将简单的文本“Hello”保存到文件中。我检查了在线文档和一些书籍(20 个 phonegap 食谱),但它仍然给我一个错误警报。以下是我的代码:

function SavetoFile(name, surname, mobilenumber, email, language, addressline1, addressline2, locality){

var fileWriter;

saveFile();

function onLoad(){
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSComplete, fail);            
}

function onFSComplete(fileSystem){ // load file, create it if it doesn't exist
    fileSystem.root.getFile("userdetails.txt", {create:true}. onFileEntryComplete, fail);   
}

function onFileEntryComplete(fileEntry){ // set up a file writer
    fileEntry.createWriter(onFileWriterComplete, fail); 
}

function onFileWriterComplete(fileWriter){ // store the file writer in a global variable for when user presses save
    fileWriter = fileWriter;        
}

function saveFile(){

    if(fileWriter != null){
        fileWriter.onwrite = function(evt){
            alert("Details Saved Successfully");                
        }

        var fileline = "Hello";
        fileWriter.write(fileline);

    }
    else{
        alert("There was an error trying to save the file");    
    }
    return false;

}

function fail(error) {
    alert(error.code);
    }   

}

我正在调用主函数SavetoFile()并传递不同的参数。但是在使用它们之前,我想通过保存单词“Hello”来进行较小规模的测试,因此我在主函数中调用了另一个方法saveFile() 。这仍然给我一条错误消息。知道问题的根源可能是什么吗?

4

1 回答 1

0

调用函数 SavetoFile 时要小心。加载文档后调用它,使用

document.addEventListener('deviceready',SavetoFile,false);

也就是说,这样做是为了调用 SavetoFile 函数

于 2013-05-09T09:00:49.263 回答