JavaScript cannot directly write a file to a server, but if if your server is using PHP you can use this technology.
See
PHP 5 File Create/Write[
^]
You can then use AJAX
XMLHttpRequest()[
^] to call the PHP function
var function saveFile(fileName, xmlDocument) {
var data = new FormData();
data.append("fileName", fileName);
data.append("content", xmlDocument);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4 && xhr.status == 200)
{
alert(xhr.responseText);
}
};
xhr.open('post', 'test.php', true);
xhr.send(data);
}
PHP
<?php <br mode="hold" /?>if (!empty($_POST['data']))
{
$fpath = $_POST['fileName'];
$file = fopen($fpath, 'w') or die("Unable to open file $fpath"); // UPDATE
$content = $_POST['content'];
fwrite($file, $content);
fclose($file);
echo "The file " + $fpath " was saved.";
}
?>
This is just a small example without any error handling, so just use it as a starting point.