Creating a downloadable text-file using blobs
Created as part of my answer to http://stackoverflow.com/questions/8178825/create-text-file-in-javascript
HTML
<textarea id="textbox">Type something here</textarea> <button id="create" onclick="call()">Create file</button> <a download="info.txt" id="downloadlink" style="display: none">Download</a>
JavaScript
alert('test');
function makeTextFile(text) {
var textFile = null;
var data = new Blob([text], {type: 'text/plain'});
// If we are replacing a previously generated file we need to
// manually revoke the object URL to avoid memory leaks.
if (textFile !== null) {
window.URL.revokeObjectURL(textFile);
}
textFile = window.URL.createObjectURL(data);
return textFile;
};
function call(){
var create = document.getElementById('create');
var textbox = document.getElementById('textbox');
var link = document.getElementById('downloadlink');
link.href = makeTextFile(textbox.value);
link.style.display = 'block';
};