Skip to content

Instantly share code, notes, and snippets.

@davalapar
Created July 31, 2018 12:06
Show Gist options
  • Save davalapar/4da7b175e107faef5ce7c9723aaf0663 to your computer and use it in GitHub Desktop.
Save davalapar/4da7b175e107faef5ce7c9723aaf0663 to your computer and use it in GitHub Desktop.
BlobSaving.js
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);
// OR
// https://stackoverflow.com/a/48374078
blobGeneratingFunction.then(blob => {
let a = document.createElement("a")
let blobURL = URL.createObjectURL(blob)
a.download = 'test.png'
a.href = blobURL
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment