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;
        }
4
  • 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 Commented Jun 5, 2020 at 0: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. Commented Jun 5, 2020 at 0:56
  • MDN web docs 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. Commented Jun 5, 2020 at 1:02
  • From the duplicate, ignore any answer that suggests you can pass in a file:/// path. They are incorrect Commented Jun 5, 2020 at 1:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.