0

I know that we can read a file using filesystem api, Im doing something like this :

test.html file

<input type="file" onchange="readText(event)" />
<pre id="output"></pre>

<script>
  async function readText(event) {
    const file = event.target.files.item(0)
    const text = await file.text();

    document.getElementById("output").innerText = text
  }
</script>

So now what I want to do, it's to read by defaut the same file (test.html) and don't need to choose the file.

The different step :

  • I open a browser
  • then test.html file
  • display me the content of same file without click on choose file

How can I do that ?

Thanks for your help :)

zefzef aa
  • 67
  • 6
  • It will NOT be allowed in the browser, as it would be a huge security risk..., imagine opening up a page to see that it can read your whole computer. What you have done, is the best of what is possible, without causing a huge security risk. – tsamridh86 Jul 23 '21 at 10:53
  • On your own computer only? Then you can start your Chrome browser with the `--allow-file-access-from-files` flag. Then you'll be able to fetch your file through an XMLHttpRequest (beware, `fetch` will still not work). Otherwise if you are using Firefox, you don't need a flag, but you need the file to be in the same directory tree as your html file. Finally, you could also use IndexedDB to store your file in the browser's memory, this way you would have to select it only once, and then you could grab (a copy) at next launch. – Kaiido Jul 23 '21 at 11:11

0 Answers0