2013-05-09 54 views
0

我想將一個簡單的文本「你好」保存到使用phonegap filewriter API的文件中。我和一些書一起檢查了在線文檔(20個phonegap的食譜),但它仍然給我一個錯誤警報。下面是我的代碼:Phonegap javascript文件保存

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()。這仍然給我一個錯誤信息。任何想法問題的根源可能是什麼?

回答

0

保存時調用函數SavetoFile。加載文檔後 調用它,使用

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

是,這樣做爲了調用的SaveToFile功能