Last active
February 18, 2019 21:52
-
-
Save bspavel/1f9dac4638290ba04fb11391aebd49fc to your computer and use it in GitHub Desktop.
JavaScript Download (Save AS)
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
// https://stackoverflow.com/questions/23451726/saving-binary-data-as-file-using-javascript-from-a-browser | |
var sampleBytes = new Int8Array(4096); | |
var saveByteArray = (function () { | |
var a = document.createElement("a"); | |
document.body.appendChild(a); | |
a.style = "display: none"; | |
return function (data, name) { | |
var blob = new Blob(data, {type: "octet/stream"}), | |
url = window.URL.createObjectURL(blob); | |
a.href = url; | |
a.download = name; | |
a.click(); | |
window.URL.revokeObjectURL(url); | |
}; | |
}()); | |
saveByteArray(["sampleBytes"], 'example.txt'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment