Skip to content

Instantly share code, notes, and snippets.

@rendfall
Created March 18, 2018 22:10
Show Gist options
  • Save rendfall/508fe6d9bf276e859de3be39da3d028c to your computer and use it in GitHub Desktop.
Save rendfall/508fe6d9bf276e859de3be39da3d028c to your computer and use it in GitHub Desktop.
BLOB saving as JSON
// https://jsfiddle.net/koldev/cW7W5/
var saveData = (function () {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
return function (data, fileName) {
var json = JSON.stringify(data),
blob = new Blob([json], {type: "octet/stream"}),
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
};
}());
var data = { x: 42, s: "hello, world", d: new Date() },
fileName = "my-download.json";
saveData(data, fileName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment