2

I try to build an offline, client-only-based app. Say I have two files, data.txt and read.html, both in the same directory.

Now all I want to do is to read (in read.html) the content of data.txt and, say, show it in a DIV (without any user interaction - so there is no <input type="file"> or anything).

I have read many articles on this, all saying that it is not possible due to security issues to open local files. But I am not sure whether this is true for an offline app. For example, I can "almost" achieve want I want by the following code:

var w = window.open("data.txt");
var content = w.document.body.textContent;
mydiv.innerHTML = editForPresentation(content);
w.close();

The only problem is that this works only for .txt files but not for e.g. .xlsx files because the browser (I use Edge) does not open the latter as plaintext.

There must be a simple method to do this that I overlook, right?

Remirror
  • 692
  • 5
  • 14

1 Answers1

-1

No there isn't. Javascript on client-side doesn't have any access to the file system. Period. Final.

What you can do if you want to use javascript only is creating a server aplication with Node.js (which is a "server side" js library) or any other js server-side library and use it as a base for what you want. Then you access the server with your javascript client-side and ask for the file that you want. But you must have 2 sides of the aplication. Server AND client. Not only client.

window.open opens a resource on a browser window, that's why it works. It's like you're loading a new page. Binary resources however won't load. You can also use <script src='something'> to load js files and they'll load and run. But none of the files loaded will have access to the file system.

Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73
  • Then why does my example work? It would be sufficient to tell the browser: "read this file as plaintext". – Remirror Apr 03 '17 at 16:11
  • I have to return the question back to you... why does it work only with plaintext files ? think about it. Imagine the security implications of someone loading a binary file just by accessing a site, then be able to use javascript to save a file in the system. It's easy to see the level of caos that could emerge if this was possible. Forget about it. Study Node.js and you'll grasp it better. – Nelson Teixeira Apr 03 '17 at 16:19
  • Hmm I see. Still, I wonder how the browser can dinstinguish between a binary file and a plaintext file. Only by the file extension, right? So, why can't I tell him to ignore this and to regard everything as plaintext. – Remirror Apr 03 '17 at 16:31
  • It's not by extension only. But this is a good question. Why don't you ask a new question asking why you can't point a – Nelson Teixeira Apr 03 '17 at 16:35
  • 1
    I did some experiments. For the case of – Remirror Apr 03 '17 at 16:49
  • You can read local files using [FileReader](https://stackoverflow.com/a/3582755/975097), but only if they are selected by the user. – Anderson Green Jul 12 '21 at 17:20