meexplorer meexplorer - 29 days ago 13
Javascript Question

Read / Write txt file on server using only javascript without involving any server side language

I'm working on a simple HTML/Javascript application. This application shows a random text message to the user in a text area. I have 15-20 such text messages of at most 500 characters. I don't want to save these messages in database. I have two scenarios -



  1. I have multiple .txt files for each message and then by using
    javascript only to read a random txt file and display file's content
    in textarea.

  2. User enters a message in a separate text area and
    by pressing save button, a new txt file for that message should be
    created.



I've been searching how to read / write files in javascript, I found these two posts -
Is it possible to write data to file using only JavaScript? and How to read and write into file using JavaScript.
But these posts are about reading file on client side or reading file from file input type.


http://stackoverflow.com/a/21012689/1930283

This suggestion is about using some server side language.
Is there any way to read and write txt files using Javascript only.


Answer

Only if JavaScript is your server side language (e.g. with Node.JS).

Servers don't let you write files to them by default. That would be a horrible security problem.

Comments