Skip to main content

Thread: HTML/Javascript - Saving files


i'm working on editor, kind of bespin, using javascript , html5.

have few questions:

1. how output content of variable storing text, file using javascript or possible?

2. if can't js, can language wouldn't require server processing?

the quick answer here is: no , no. js or other client-side scripting languages cannot write files directly afaik. you'll need server this.

anyway, want save data? on server or on user's computer? i'm guessing latter...


here's simple example of case want user download text using php , jquery. note quite roundabout, since data sent server , sent user, there waste of round-trip on network.

php code:
<?php
if (! empty($_get['save']) && ! empty($_get['data'])) {
    
header('content-type: text/plain');
    
header('content-disposition: attachment');
    echo 
$_get['data'];
    die;
}
?>
<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
        <script type="text/javascript">
            var string = "hello world";
            $(document).ready(function () {
                $('button').click(function () {
                    location.href = 'test.php?save=1&data=' + encodeuri(string);
                });
            });
        </script>
    </head>
    <body>
        <button>download variable</button>
    </body>
</html>

saving on server need combination of js , server-side scripting language.

here's simple example in jquery , php using ajax case 1 (saving server):
php code:
<?php
if (! empty($_get['save']) && ! empty($_post['data'])) {
    
// do something with $data, save to filesystem for example:
    // file_put_contents('myfile.txt', $data);
    
echo 'ok';
    die;
}
?>
<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
        <script type="text/javascript">
            var string = "hello world";
            $(document).ready(function () {
                $('button').click(function () {
                    $.post('test.php?save=1', {'data' : string}, function (data) {
                        var responce = "the upload failed";
                        if (data == 'ok') {
                            responce = "the upload succeded";
                        }
                        alert(responce);
                    });
                });
            });
        </script>
    </head>
    <body>
        <button>upload variable</button>
    </body>
</html>


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [SOLVED] HTML/Javascript - Saving files


Ubuntu

Comments

Popular posts from this blog

How to change text Component easybook reloaded *newbee* - Joomla! Forum - community, help and support

After Effect warning: A problem occurred when processing OpenGL commands

Preconditions Failed. - Joomla! Forum - community, help and support