Search code examples
javascriptreadfile

Can JavaScript read files on the hard drive?


Here I have learned that it is possible with JavaScript in HTML to create files offline and save them on the hard disk.

But what about reading files? Can JavaScript access, say one of my .txt files on my self phone and display the read data (or at least place it in the local storage)?

I know that it is possible with PHP, but there is no PHP on my cellphone, while HTML is always there.


Solution

  • You can use <input type="file"> and FileReader to read files stored at local filesystem, see File API; also How FileReader.readAsText in HTML5 File API works?.

    If trying at Chromium or Chrome to read file: protocol launch the browser with --allow-file-access-from-files flag, without other Chrome instances running, or use a different --user-data-dir, see Read local XML with JS.