Created
September 17, 2014 06:30
-
-
Save kawa-/0b7ea24c20f2f5231619 to your computer and use it in GitHub Desktop.
Generating a txt file using browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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