views:

38

answers:

1

I am looking for a way to save the contents of an HTML5 web database to a local file for persistence and for loading back into the web application. I'm using Firefox 3.6.8 with the HTML5 features to use a local web database. I can upload a file and read it's contents but am unable to find an elegant way to save the contents of the database to the file system. Any ideas?

I believe there is something written in flash that can do this. I would consider this but I would prefer a straight HTML 5 version instead.

A: 

You can't do this with plain HTML 5, as it doesn't have any mechanism to deal with local files (that would imply a security risk anyway). You'll at least need some JavaScript or Flash to store data locally.

But you can't access the local filesystem using JavaScript. You can only use cookies or the localStorage capabilities of a browser.

Nor can you do this using Flash. Both system work the same: they store information in a place specified by the browser/Flash. Moreover, in both cases space is limited (the limit varies between platforms and even individual settings).

The only way to do this is to post the data to the server, e.g., using a hidden form, and let the server respond with header Content-Disposition: attachment and sending the file back to the client. For example:

<form action="doLoopback.php" method="POST" name="theForm">
  <input type="hidden" id="data-container" name="data">
</form>
<span id="trigger">→ Click here! ←</span>
<script>
var theData = '123, 2334, "Fé℗⇒oo"';

function sendData() {
    document.getElementById('data-container').value = theData;
    document.theForm.submit();
}

window.onload = function () {
    var el = document.getElementById('trigger');
    el.onclick = sendData;
};
</script>

and using PHP the response file reads:

<?php
    if (isset($_POST['data'])) {
        header('Content-Disposition: attachment; filename=foo.csv');
        header('Content-Type: application/octet-stream');
        echo $_POST['data'];
    }
?>
Marcel Korpel
What I'm looking for is a way to keep everything on the client. I don't want any of the data being sent to the server. I believe I've seen a flash app that could do this. Any ideas what that is called?I also understand why they haven't added the ability to save to disk due to it being a security risk, but if I can download files from the server and save them locally, why is clicking on a link that performs the exact same file-save dialog locally any more of a security risk?
spig
@spig: You're right about your last remark, I thought of it myself. It's just that what you describe is not the way browsers were designed. Offline web-apps are rather new. I don't know Flash, so I can't help you with that.
Marcel Korpel
Maybe these would be good references for the question:How to save a file locally in flash:http://cookbooks.adobe.com/post_Save_a_file_locally_with_Flash_Player_10-14426.htmlHow to save a file locally in silverlight:http://www.kirupa.com/blend_silverlight/saving_file_locally_pg1.htm
spig
It looks like it's something that will be coming: http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2009-August/022118.html
spig
I guess in reality that is still downloading from the server but doing so without using a "Save-as" dialog. I guess what I really want is a little more than that - or at least the ability to save a local file from the client side.
spig
@spig: Indeed, I didn't look at the WHATWG mailing list yet, but the linked proposal is about something different (download from the server using drag-and-drop), also notice the consideration: "We should consider allowing only http and https typed URL in the associateddata for the "DownloadURL" format." I think this is more what you're looking for: http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2009-August/022122.html
Marcel Korpel