0

I want to read the text file from javascript but fileReader.onload is not getting called hence str is coming as null.

if (window.File && window.FileReader && window.FileList && window.Blob)
{
    var fileReader = new FileReader();
    fileReader.onload = function(e) {
      text = fileReader.result;
    };  
    var file = new File([""],"C:\\Users\\Swap\\Desktop\\1.txt");
    fileReader.readAsText(file); 
    var str = text;
    alert(str); 
} 
else 
{ 
     alert("Files are not supported"); 
} 
Swapnil
  • 1,270
  • 13
  • 28
  • It doesn't get called because you only declare `text` *implicitly* as a global by assigning to it, but you do that in the `onload` function and the JS dies with an exception because you try to read from `text` before you declared it. – Quentin Jul 10 '18 at 10:34
  • Your file is an empty string anyway (`[""]`) so there is nothing useful to read. – Quentin Jul 10 '18 at 10:34

0 Answers0