HTML5 FileReader() functions not getting called
I can't figure out why neither readSuccess()
or readFailure()
are getting called in the following:
function readMyFile(){
var reader = new FileReader();
reader.onload = readSuccess;
reader.onerror = readFailure;
reader.readAsText("test.txt");
function readSuccess(evt){
alert(evt.target.result);
}
function readFailure(evt) {
alert("Did not read file!");
}
}
When I step though the code in the Chrome javascript debugger, 开发者_如何学Cit steps past the reader.readAsText("test.text");
command, but then exits the whole function, never calling readSuccess()
or readFailure()
You can't specify a file with a string in reader.readAsText()
, it needs to be a reference to a Blob
: see the documentation.
You should be getting the Blob
from a file-type input field, check out these awesome examples.
精彩评论