File API を使った下記のようなJavascriptのソースコードを作りました。読み込み関数のgotFS_read(fname)で読み取ったテキストデータを関数の戻り値として受けたいのですが、うまくいきません。何か良い方法はないでしょうか?
//読み込み
var res = gotFS_read('textfile');
$('#return').innerText(res);
// ファイル読込処理
function gotFS_read(fname) {
this.text = '';
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, read, fail);
function read(fileSystem){
fileSystem.root.getFile(fname, {}, function(fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
// ここに読み込み完了後の処理を書く
console.log(evt.target.result);
this.text = evt.target.result;
console.log(this.text);
$("#textarea").val(this.text);
alert("正常にテキストを読み込みました。");
return this.text;
};
reader.readAsText(file, "utf-8");
}, fail)
}, fail);
}
}