I want to get content of text file. In addition, how can I use in JavaScript? e.g line by line or array?
Asked
Active
Viewed 3.6k times
0
-
2server side or client side? – Mulan Jul 26 '16 at 21:58
-
3Related : http://stackoverflow.com/questions/13709482/how-to-read-text-file-in-javascript – Jul 26 '16 at 22:00
-
1Best to use AJAX for this. There is also a `FileReader` JavaScript Constructor, but it won't work on all Browsers. – StackSlave Jul 26 '16 at 22:05
-
PHPglue only if there is a purpose for transferring the file contents... Plus, the acronym AJAX has lost almost all of its meaning. – Mulan Jul 26 '16 at 22:10
-
Oh, you edited your comment. Yes, `FileReader` would still be asynchronous but we can stop saying AJAX as a catch-all for anything that is asynchronous. – Mulan Jul 26 '16 at 22:12
2 Answers
1
It seems that you target client-side .
Then if your text file is hosted under the same project where page HTML contains or import the following script :
$.get('/docs/file.txt',{},function(content){
let lines=content.split('\n');
console.log(`"file.txt" contains ${lines.length} lines`)
console.log(`First line : ${lines[0]}`)
});
Assume that if we have the following routing rule :
http://localhost/
--->/var/www/html/index.html
/docs/file.txt
should follow this rule :
http://localhost/docs/file.txt
-->/var/www/html/docs/file.txt

Abdennour TOUMI
- 87,526
- 38
- 249
- 254
1
Checkout the node fs
module for this. You can read and write content both sync and async depending on what you're trying to do with it.

Mulan
- 129,518
- 31
- 228
- 259

Matthew Kory
- 19
- 1