Skip to content Skip to sidebar Skip to footer

How To Write A Text File On Server Using Javascript?

I would like to change the contents of a file present on the server using only JavaScript code. Also, feel free to consider that the server grants write access to that file, if nee

Solution 1:

Node.js is the best for this. It gives you acces to your local file system.

Example:

var fs = require('fs');
fs.writeFile("/tmp/test.txt", "Hey there!", function(err) {
    if(err) {
        console.log(err);
    } else {
        console.log("The file was saved!");
    }
}); 

Solution 2:

In browser JavaScript runs on the client side, for obvious reasons you can't write files to the server side. You can use JavaScript via node.js to write server side code, but you will still need to pass the data from the browser to the server.

Solution 3:

JavaScript in the browser runs on the client, so you will need the help of a server side language. You can still use JavaScript on the server with node.js if want to stick with JavaScript only.

Solution 4:

You need to use a server-side language.

If you really want to do that using Javascript, then check Node.js. Here's a tutorial to create a file on the server using Javascript and Node.js:

http://docs.nodejitsu.com/articles/file-system/how-to-write-files-in-nodejs

Post a Comment for "How To Write A Text File On Server Using Javascript?"