Skip to content

Instantly share code, notes, and snippets.

@kazuya-k-ishibashi
Last active December 29, 2017 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazuya-k-ishibashi/9d6c1afa8e3a25bc81b8c3e49d0dde57 to your computer and use it in GitHub Desktop.
Save kazuya-k-ishibashi/9d6c1afa8e3a25bc81b8c3e49d0dde57 to your computer and use it in GitHub Desktop.
download text file by browser.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>log parse</title>
</head>
<body>
<textarea id="input"></textarea>
<button id="download" type="button">download</button>
<script>
document.getElementById("download").addEventListener("click", function() {
var inputtext = document.getElementById("input").value
var filename = "download_sample.txt"
saveFile(inputtext, filename)
}, false)
function saveFile(contents, filename) {
var filename_ = filename || "file.txt"
var blob = new Blob([ contents ], { "type": "text/plain" })
var msSaveBlob = window.navigator.msSaveBlob
if (msSaveBlob) {
msSaveBlob(blob, filename)
} else {
var a = document.createElement("a")
a.href = URL.createObjectURL(blob)
a.target = "_blank"
a.download = filename
a.click()
URL.revokeObjectURL(a.href)
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment