Save as File Demo
HTML
<div id="content">
<textarea id="output_text" name="output_text" class="output_text" cols="50" rows="5" wrap="off">
What is the
Question
to all Answers?
:-)</textarea>
<a class="button" type="u" id="saveOutput" href="#" onClick="saveOutput(this)">Sadsf as File</a>
</div>
CSS
.button {
display: block;
width: 20em;
background-color: rgba(100, 100, 100, 0.8);
border-radius: 1em;
padding: 0.5em;
margin: 0.5em;
text-align: center;
cursor: pointer;
color: black;
}
textarea {
padding: 0.5em;
}
JavaScript
function saveAsFile(link, content, filename) {
// https://developer.mozilla.org/en-US/docs/Web/API/Blob
// var aFileParts = ['<a id="a"><b id="b">hey!</b></a>'];
// var oMyBlob = new Blob(aFileParts, {type: 'text/html'}); // the blob
// http://stackoverflow.com/a/16330385/574981
var blob = new Blob([content], {type: "application/json"});
var url = URL.createObjectURL(blob);
console.log("update download link:");
// var a = document.createElement('a');
var a = link;
a.download = filename + ".txt";
a.href = url;
// a.textContent = "Download File";
// console.log("download link:", a);
// console.log("open download link:", a);
// a.click(); // this has not worked for me..
//a.remove();
}
function saveOutput(link){
console.log("saveOutput clicked");
saveAsFile(link, document.getElementById("output_text").value, "HelloWorldFile");
}