Skip to content

Instantly share code, notes, and snippets.

@alexfinnarn
Created October 25, 2018 20:54
Show Gist options
  • Save alexfinnarn/30cbfbfa6a3cb42eef6beba8d35b62a1 to your computer and use it in GitHub Desktop.
Save alexfinnarn/30cbfbfa6a3cb42eef6beba8d35b62a1 to your computer and use it in GitHub Desktop.
function downloadFile(finalData) {
const data = new Blob([JSON.stringify(finalData, null, 2)], {type : 'application/json'});
if (navigator.msSaveBlob) {
// IE 10+.
navigator.msSaveBlob(data, 'directory.json');
} else {
const link = document.createElement('a');
if (link.download !== undefined) {
// Feature detection for Browsers that support HTML5 download attribute.
const url = URL.createObjectURL(data);
link.setAttribute('href', url);
link.setAttribute('download', 'directory.json');
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment