Created
December 14, 2015 09:12
-
-
Save eagafonov/8ee5d7deea6c5ebddcc5 to your computer and use it in GitHub Desktop.
Save a file from browser using File API (http://www.w3.org/TR/FileAPI/)
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
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