Can JavaScript read data from text file? So I have a login page and I want to know if JavaScript can read data stored in the text file so each time the user login it reads the data from text file and allow the user to login. Note:It's just for testing purpose.
Asked
Active
Viewed 211 times
0
-
Would this be JS running in a browser (client side) or running in Node.js (or similar: server side)? – Richard Nov 26 '18 at 11:36
-
1Javascript cannot access files from your desktop as this would be a huge security issue. Uploading the text file (ie to a server) and using Node.js could be a way around this... – Nick Parsons Nov 26 '18 at 11:37
-
@Richard it would be JS running in a browser. – eme Nov 26 '18 at 11:40
-
@NickParsons I just need it for a test. – eme Nov 26 '18 at 11:41
-
You can use [cookies](https://www.w3schools.com/js/js_cookies.asp) or [web storage](https://www.w3schools.com/html/html5_webstorage.asp) – Baro Nov 26 '18 at 11:48
-
You can read file uploaded through input type file. But you cannot access the local file system. – Mukesh Verma Nov 26 '18 at 11:49
-
Possible duplicate of [How to read an external local JSON file in JavaScript?](https://stackoverflow.com/questions/19706046/how-to-read-an-external-local-json-file-in-javascript) – Dracontis Nov 26 '18 at 13:40
1 Answers
0
(With the clarification in the comments to the question.)
This is not possible.
The ability of javaScript running in a browser to read arbitrary files from the local system is prevented to avoid a security hole.
(Note, there are proposed extensions to JavaScript to allow this, but the user would need to select the file explicitly and the JS code would not even have access to the filename: this is not an automated solution to storing some data locally.)

Richard
- 106,783
- 21
- 203
- 265