I want to use PhoneGap to read a file then return me the data of that file, but when i execute the code, it return before read the file. The code below shows how to read file:
function getData() {
var fileData = null;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
function gotFS(fileSystem) {
fileSystem.root.getFile("data.txt", null, gotFileEntry, fail);
function gotFileEntry(fileEntry) {
fileEntry.file(gotFile, fail);
}
function gotFile(file){
readAsText(file);
}
function readAsText(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
fileData = evt.target.result;
alert(fileData)
// this alert is executed after returning the data, so it return the data, but the second alert return null when this is method called.
};
reader.readAsText(file);
}
}
return fileData;
}
Now when i call that function, it will return null
var data = getData();
alert(data); // return null