jack lanza
jack lanza

Reputation: 299

save javascript output as textfile

this is my xml file:-

<child_2 entity_id="2" value="Root" parent_id="1">
    <child_4 entity_id="4" value="Activities" parent_id="2">
        <child_10066 entity_id="10066" value="Physical1" parent_id="4">
            <child_10067 entity_id="10067" value="Cricket" parent_id="10066">
                <child_10068 entity_id="10068" value="One Day" parent_id="10067"/>
            </child_10067>
        </child_10066>
        <child_10069 entity_id="10069" value="Test2" parent_id="4"/>
        <child_10070 entity_id="10070" value="Test3" parent_id="4"/>
        <child_10071 entity_id="10071" value="Test4" parent_id="4"/>
        <child_10072 entity_id="10072" value="Test5" parent_id="4"/>
        <child_5 entity_id="5" value="Physical" parent_id="4"/>
    </child_4>
</child_2>

this is my javscript:-

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
var xml;
$.get(
    "region.xml",  
    function(data)
    { xml=data; },
    "html"
);
function get_list(){
    xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc ),
    $title = $xml.find('[entity_id="'+$('#select').val()+'"]');
    $nodes = $title.find('*');
    var result='';
    $nodes.each(function(){
        result += $(this).attr('entity_id');
        result += ',';
    });
    $("#result").html(result);
}
</script>
<input type="text" id="select">
<input type="button" name="button" value="Search" onclick="get_list()" >
<div id="result">
</div>


here i am try to get all the xml attribute value i am success on them now i want to save this output as a textfile
how its possible
please help me out with this...
thanks

Upvotes: 3

Views: 4897

Answers (4)

Prasath K
Prasath K

Reputation: 5018

Have a look on

  1. File System API of HTML5

  2. File Writer API link

Upvotes: 1

TheBrain
TheBrain

Reputation: 5608

One HTML5 way of doing it:

window.URL = window.URL || window.webkitURL;

function download(/*string*/dataToDownload, /*string*/filename) {
     var a = document.createElement('a');
     var blob = new Blob(dataToDownload, {'type':'application\/octet-stream'});
     a.href = window.URL.createObjectURL(blob);
     a.download = filename;
     a.click();
};

You can also use Downloadify library, that uses flash to trigger the download and allow the user to name the file.

Or the most common and safe way is to send the data to php and return it with the correct headers that will trigger the download and also allow the user to name the file

Upvotes: 1

Tam&#225;s Pap
Tam&#225;s Pap

Reputation: 18273

If you want a solution that works cross browser, it's better to post the data to server, to a PHP file for example. This php file can then "offer" the file to download.

See this question to get started: How to force file download with PHP.

Upvotes: 0

Alytrem
Alytrem

Reputation: 2620

Writing to a file on client side is not only quite complicated (use ActiveX Scripting.FileSystemObject), but is also a bad practice due to security reasons.

Upvotes: 0

Related Questions