Skip to content

Instantly share code, notes, and snippets.

@kawa-
Created September 17, 2014 06:30
Show Gist options
  • Save kawa-/0b7ea24c20f2f5231619 to your computer and use it in GitHub Desktop.
Save kawa-/0b7ea24c20f2f5231619 to your computer and use it in GitHub Desktop.
Generating a txt file using browser
<script type="text/javascript">
function downloadFile() {
var filename = document.getElementById("filename-input").value;
var text = document.getElementById("text").value;
console.log("filename:");
console.log(filename);
console.log("text:");
console.log(text);
var blob = new Blob([text], {type: "text/plain"});
var url = window.URL.createObjectURL(blob);
document.getElementById("download-link").href = url;
document.getElementById("download-link").download = filename;
}
</script>
<p>
Filename: <input type="text" id="filename-input" value="filename-here.txt">
</p>
<p>
<textarea id="text">Input something...</textarea>
</p>
<p><a href=# id="download-link" onclick="downloadFile();">Click to download</a></p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment