0

I want to get content of text file. In addition, how can I use in JavaScript? e.g line by line or array?

5
  • 2
    server side or client side? Commented Jul 26, 2016 at 21:58
  • 3
    Related : stackoverflow.com/questions/13709482/… Commented Jul 26, 2016 at 22:00
  • 1
    Best to use AJAX for this. There is also a FileReader JavaScript Constructor, but it won't work on all Browsers. Commented Jul 26, 2016 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. Commented Jul 26, 2016 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. Commented Jul 26, 2016 at 22:12

2 Answers 2

2

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.

https://nodejs.org/api/fs.html

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.