Skip to content

Instantly share code, notes, and snippets.

@bspavel
Last active February 18, 2019 21:52
Show Gist options
  • Save bspavel/1f9dac4638290ba04fb11391aebd49fc to your computer and use it in GitHub Desktop.
Save bspavel/1f9dac4638290ba04fb11391aebd49fc to your computer and use it in GitHub Desktop.
JavaScript Download (Save AS)
// 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