-1

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.

brilliant
  • 2,805
  • 11
  • 39
  • 57
  • 1
    how would the javascript access a txt file on your cellphone? How would it know where to look? There aren't really file directories, nonetheless standardized file storage. – Wold Oct 23 '17 at 01:42
  • 1
    @Wold: do you mean "let alone"? – Ry- Oct 23 '17 at 01:45
  • The short answer is no, because of the secure sandboxing the browser does around the HTML + JavaScript. Even the successful (non-deprecated) filesystem-ish APIs like LocalStorage don't give you unfettered access to the filesystem. JavaScript under Node.js could do this with no problem, however. –  Oct 23 '17 at 01:47
  • At least, the manifest, a cookie, else no. – pirs Oct 23 '17 at 01:51
  • @Wold - Can you, please, elaborate on standardized file storage? What is it? – brilliant Oct 23 '17 at 01:57
  • @hacksalot - Is Node.js one of the commands in JavaScript? (Sorry if my question is too stupid - mt knowledge of JavaScript is very primitive) – brilliant Oct 23 '17 at 01:58
  • @pirs - Do you mean that HTML can still leave some info on my cellphone as a cookie? What is manifest? – brilliant Oct 23 '17 at 01:59
  • @brilliant https://developer.mozilla.org/fr/Add-ons/WebExtensions/manifest.json – pirs Oct 23 '17 at 02:00
  • @pirs - Thanks for that link! – brilliant Oct 23 '17 at 02:04
  • 1
    You can use `` and `FileReader` to read files stored at local filesystem, see [File API](https://w3c.github.io/FileAPI/) – guest271314 Oct 23 '17 at 02:26
  • @Ryan yah that's embarrassing. I'm not too familiar with storage on android, but there are third party applications on ios that allow file storage, where you can interact with them from the web. But there isn't really any plain file storage that I'm aware of. – Wold Oct 23 '17 at 02:42
  • @guest271314 - Is that some kind of application that I need to install on my cellphone first? – brilliant Oct 23 '17 at 03:09
  • @brilliant No, though you would need to compose the HTML and JavaScript. 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](https://stackoverflow.com/questions/41279589/read-local-xml-with-js/) – guest271314 Oct 23 '17 at 03:14
  • @guest271314 - I see. Thanks a lot. It looks quite promising! – brilliant Oct 23 '17 at 03:21
  • @guest271314 - Thanks again. Actually, your comment is a valid answer and a solution to my problem. Why don't you re-write your comment as an answer and I will give the right credit. – brilliant Oct 23 '17 at 17:51

1 Answers1

1

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.

guest271314
  • 1
  • 15
  • 104
  • 177