0

I need to get a txt file's text. I have found that sometimes it works sometimes it doesn't.

var reader = new FileReader();
var fileInput = document.getElementById('fileInput').files[0];
console.log(reader.valueOf());

But when it does work, one of the values in the map is "result" and it has the text of the txt file, so why doesn't it work regularly and how to get the value of "result". Thanks in advance.

ds_secret
  • 338
  • 3
  • 18

1 Answers1

0

The proper way to get the result of FileReader is reader.onload=function() {/*retrieve reader.result here*/};, and you can insure that you can retrieve the result of the reader reader.result once the file you're trying to load is successfully loaded.

To invoke the reader, call reader.readAsText(fileInput).

btooom
  • 53
  • 4