0

I have a html page on my computer and want to load text into it from a plain text file. Every result from searching this on internet showed how to have a html page ask for a file, which is not what i want. I have already a text file in my computer which i want to read from, how can this be done?

Turns out this cannot be done. So how else can we accomplish having different information on a webpage each time?

1 Answers1

1

It's not possible. Even if you know the path to the file you want, you cannot open it from a browser. (If one could, it'd be a security risk - the opening of a local page would be able to view any data on your computer.)

You have to get the user to select the file to be read deliberately.

CertainPerformance
  • 313,535
  • 40
  • 245
  • 254
  • Then how can i make the page show a different thing each time, depending on a file? There has to be a way to do that. And only i access that page anyway... – Carla is my name Mar 14 '21 at 21:30
  • In order to read the file, the user will have to choose it themselves. Once they've chosen a file, you can read it (using whatever method / format you want - maybe JSON?) and show out the appropriate thing as a result. If this is a big problem for you, consider putting it on an external server, or setting one up on your localhost, and then fetch from an endpoint on the localhost, which'll be permitted. (You'll need a backend, eg Node) – CertainPerformance Mar 14 '21 at 21:33
  • I don't know what you're trying to accomplish, but depending on the situation, you might even be able to do it without a server or localhost, and just by using Local Storage (while running your script/page via a userscript instead of with a local file) – CertainPerformance Mar 14 '21 at 21:36