Click here to Skip to main content
15,888,323 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I have created XML document to save webpage data.

var xmlDoc = document.implementation.createDocument("", "", null);


how can I store (upload) this on my server.

thanks very much.
Posted
Comments
Mohibur Rashid 12-Nov-15 23:52pm    
try this
link

1 solution

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
JavaScript
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); // UPDATE
        }
    };
    xhr.open('post', 'test.php', true); // Don't forget to change file name
    xhr.send(data);
}


PHP
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.
 
Share this answer
 
v3
Comments
Member 11302528 13-Nov-15 10:37am    
so thankyou but even FileSaved alert was prompted, i can't find any new file was saved on server.

I put your phpCode in test.php. Does that right ?
George Jonsson 13-Nov-15 19:47pm    
Make sure you have granted your PHP file access to write information to the server hard drive.
I also added a little error handling. See the UPDATE tags.
Member 11302528 15-Nov-15 0:00am    
Thanks so much, i have solved my problem.
this's my code:


function saveFile(FileName, XMLDocument) {
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "save.php";

var vars = "fileName="+FileName+"&content=" + (new XMLSerializer()).serializeToString(XMLDocument);
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
alert("...");
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars); // Actually execute the request
George Jonsson 15-Nov-15 0:09am    
Glad you could use my answer as a starting point to get to solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900