-3

How to write to a file in Javascript(on server)?

I also need to read the file. And here is my code:

function write()
{
var = true;
if( var = true)
{
//write file
    }
}
function read()
{
//read file
}
animuson
  • 53,861
  • 28
  • 137
  • 147
Wolf
  • 9
  • 1
  • 2

3 Answers3

0

Writing a file is not a feature of Javascript. In some recent browsers you can already read them, but it's not a good option. The best way is to read it with PHP and get the response with XMLHttpRequest.

JavaScript

var xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttp');
xhr.onload = function(){
    window.console.log(this.response);
}
xhr.open('GET','/fileReader.php?fileName=foo.txt');
xhr.send();

PHP

$content = file_get_contents($_GET['fileName']);
if($content)
    echo $content;
else echo "The file could not be loaded"
Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
  • and how to write files (in php, asp, jsp. etc. – Wolf May 26 '12 at 19:16
  • @Wolf: you cannot write files client side, beyond the browser's own capacity to download. That's a security issue. Do you like the idea of clicking on a link to a page that runs some js and writes whatever-the-heck to your system? Period, end of story, and hopefully it stays that way ;) – CodeClown42 May 26 '12 at 19:17
  • I want to write file on server – Wolf May 26 '12 at 19:19
  • And what would be the content of the file? Something that the user writes on a textarea? – Danilo Valente May 26 '12 at 19:22
  • @MarkLinus no, if(blabla = true) { //write blabla to file } – Wolf May 26 '12 at 19:33
0

You can read files by doing an AJAX request with possibly a file name or id

var xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttp');
xhr.onload = function(){
    window.console.log(this.response);
}
xhr.open('GET','/readfile.php?id=1234');
xhr.send();

You can write the file by getting the data from possibly a input of type text. Assume the input id to be "text"

var xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttp');
xhr.onload = function(){
    window.console.log(this.response);
}
xhr.open('POST','/write.php');
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send("id=someid&text=" + document.getElementById("text").value);

on the php side just get the post data and write that to the file

$id = $_REQUEST["id"];
$text = $_REQUEST["text"]
$file = fopen($id . ".txt","w"); // you can change w to a incase you want to append to existing content in the file
fwrite($file,$text);
fclose($file);

If you want Javascript to do the reading or writing, as far as my knowledge goes theres only a HTML5 File API but thats for reading files only I guess.

Yohann
  • 136
  • 2
  • 13
0

If I understood your question correctly, you wish to read/write files in the server and your server side language is javascript. If you are using Node, this link : http://nodejs.org/api/fs.html#fs_fs_readfile_filename_encoding_callback provides relevant information on doing the same.

lorefnon
  • 12,875
  • 6
  • 61
  • 93
  • In that case you can write server side file content using javascript running in browser, unless you have some explicit server side script that recieves an HTTP request and after processing it, writes it into a file. The solutions posted before do just that assuming the server side language is PHP. Do keep in mind the security implications associated and perform proper sanitization of the content server on the server before you write it to your filesystem. – lorefnon May 26 '12 at 19:48