I want to get content of text file. In addition, how can I use in JavaScript? e.g line by line or array?
2 Answers
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.
Comments
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
FileReaderJavaScript Constructor, but it won't work on all Browsers.FileReaderwould still be asynchronous but we can stop saying AJAX as a catch-all for anything that is asynchronous.