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
    It can't. That would be bad. You can have the user *select* a file to provide to you with an ``, but you can't pick it for them, because that'd lend itself to malicious actions. – ceejayoz Mar 14 '21 at 21:26
  • @ceejayoz only i access that page. How can i have the page show different things each time then? – Carla is my name Mar 14 '21 at 21:28
  • If your txt and HTML file are both local, you could do it. Otherwise, it's impossible. – 9pfs Mar 14 '21 at 21:31
  • 1
    Use an actual webserver, and put the file with different things there. – ceejayoz Mar 14 '21 at 21:32
  • @ceejayoz Not everyone can afford that. If you'd like, you could explain how to do that yourself. – 9pfs Mar 14 '21 at 21:32
  • 2
    Servers (well, VPSs) have the potential to be really cheap. IIRC OVH has one for ~$3/month or thereabouts – CertainPerformance Mar 14 '21 at 21:34
  • @Anonymous A local webserver is free, and even if you want it on the public internet, Google, AWS, and Microsoft (among others) each have a free tier. – ceejayoz Mar 14 '21 at 21:44
  • Yes, but they all require you to hand over payment information. I just use Glitch to host everything. – 9pfs Mar 14 '21 at 21:45

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
  • 356,069
  • 52
  • 309
  • 320
  • 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