1

I am trying to load the text from a local text file into a JavaScript variable. The file will always be the same and no input is taken from the user for this within the HTML. I get the error that parameter 1 is not of type blob for readAsText(). Here is what I have:

function readNumber(){
            var fr = new FileReader();
            fr.readAsText("C:/Users/home/Documents/Hello_World/src/main/resources/static/numbers.txt");
           var number = fr.result;
        }
Kyle S
  • 63
  • 1
  • 6
  • 1
    welcome - are you trying to do this in a browser or with node? if within the browser, you must ask the user to specify the file using a `input type=file` – Daniel A. White Jun 05 '20 at 00:54
  • You should look into the file-picker. Have the user select this file, then opening and reading it is trivial. Your current code (if it worked) would allow a website to snoop on users computers. So, in order to get access to a file local to the machine running the javascript, the user must select it. – enhzflep Jun 05 '20 at 00:56
  • [MDN web docs](https://developer.mozilla.org/en-US/docs/Web/API/FileReader) says `FileReader cannot be used to simply read a file by pathname from a file system.` So as suggested by @enhzflep, file should be selected by user before reading it using FileReader. – Susth Jun 05 '20 at 01:02
  • From the duplicate, ignore any answer that suggests you can pass in a `file:///` path. They are incorrect – Phil Jun 05 '20 at 01:07

0 Answers0