Skip to content

Instantly share code, notes, and snippets.

@73nko
Created December 31, 2018 10:00
Show Gist options
  • Save 73nko/5a0498a0dfffcfa96797bf7eed6350ae to your computer and use it in GitHub Desktop.
Save 73nko/5a0498a0dfffcfa96797bf7eed6350ae to your computer and use it in GitHub Desktop.
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