HTML5 introduces the FileWriter class. With this class you can make Blobs. (A File is an extension of a Blob.) With JavaScript you can make a Blob and for instance show it using the dataURL.
Example:
var bb = new BlobBuilder();
bb.append('some text')
var blob = bb.getBlob('text/plain');
var fr = new FileReader();
fr.onload = function(e) {
document.location = this.result; // voila the dataURL
}
fr.readAsDataURL(blob);
But that's not good enough :) I want the newly created (text) file to be downloaded. Not opened in the same or a separate window.
Is there a way? There must be. How?
(The discussion already exists in the Google Chrome group)
UPDATE
The File API has changed, because the specs have changed (or something!?). Webkit broke backward compatibility with BlobBuilder
, now called WebKitBlobBuilder
. Same example differently on jsFiddle
UPDATE
Creating Blobs now works differently again (no more append()
):
blob = new Blob(['some text'], {type: 'text/plain'});