1

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.

https://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.

Community
  • 1
  • 1
the-dumb-programmer
  • 485
  • 3
  • 14
  • 27
  • 1
    I think it is impossible. Javascript runs in browser (client) and to handle files stored in server, you'll need a server side script. – Márcio Gonzalez Apr 27 '15 at 14:15
  • @MárcioGonzalez This is probably possible in browsers that support the [File System Access API](https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API), as long as the file is on a local server. – Anderson Green Feb 16 '22 at 21:30

4 Answers4

8

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
3

You wouldn't want any write access to files on your server from the client side code, you would easily get hacked pretty quickly. But in terms of reading, you could just host the txt files in the public html directory and perform an async get request from javascript using jquery like so:

$.get("http://../file.txt", function(data) {
console.log("Here's the file data: "+data);
});

EDIT: The reason js can't manipulate files on the server by itself is because a browser can only make requests to a server using a URL and a PORT. What happens when the server gets that request is all up to the server side code or program that's hosting the files like apache.

Micaiah Wallace
  • 1,101
  • 10
  • 17
-1

If you have a server-side service like FTP or SSH running, then yes, it is possible, but it is then also possible for anyone to read your username and password from you JavaScript file and log in to your server via the service you use.
So this should never be done.

If there is no server-side service running and you have no own server-side script, then no, it is not possible. Just like it isn't possible for me to just create files on your computer.

Siguza
  • 21,155
  • 6
  • 52
  • 89
-1

I make a weblink to my server and use FileSaver.js to manually save whatever data to any file on my server via that weblink. This is simple and save, but needs manual intervention.

Rick Smith
  • 9,031
  • 15
  • 81
  • 85
user2707695
  • 126
  • 1
  • 12