Created
December 31, 2018 10:00
-
-
Save 73nko/5a0498a0dfffcfa96797bf7eed6350ae to your computer and use it in GitHub Desktop.
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
function downloadFile(data, fileName, type="text/plain") { | |
// Create an invisible A element | |
const a = document.createElement("a"); | |
a.style.display = "none"; | |
document.body.appendChild(a); | |
// Set the HREF to a Blob representation of the data to be downloaded | |
a.href = window.URL.createObjectURL( | |
new Blob([data], { type }) | |
); | |
// Use download attribute to set set desired file name | |
a.setAttribute("download", fileName); | |
// Trigger the download by simulating click | |
a.click(); | |
// Cleanup | |
document.body.removeChild(a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment