Last active
April 6, 2020 07:26
-
-
Save basith374/0a850c8c41566a053d4fa5d05c94976b to your computer and use it in GitHub Desktop.
javascript file download
This file contains hidden or 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 downloadCSV(content, filename) { | |
| download(content, 'text/csv', filename + '.csv') | |
| } | |
| /** | |
| * @param {string} content - The file content in raw string format | |
| * @param {string} type - The filetype eg. text/plain, application/json, image/svg | |
| * @param {string} filename - The filename with extension | |
| */ | |
| function download(content, type, filename) { | |
| let link = document.createElement("a"); | |
| let data = new Blob([content], { type }); | |
| let url = URL.createObjectURL(data); | |
| link.href = url; | |
| link.style.cssText += ";visibility:hidden"; | |
| link.download = filename; | |
| 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