开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜