Created
December 1, 2022 17:56
-
-
Save AwesomeZaidi/139f2ec2add8c8a30cd182c3f5a031b2 to your computer and use it in GitHub Desktop.
downloadCsv
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
export function downloadCsv(csv: any, filename: string) { | |
const blob = new Blob([csv], { type: 'text/csv' }); | |
/* taken from react-csv */ | |
if (navigator && navigator.msSaveOrOpenBlob) { | |
navigator.msSaveOrOpenBlob(blob, filename); | |
} else { | |
const dataURI = `data:text/csv;charset=utf-8,${csv}`; | |
const URL = window.URL || window.webkitURL; | |
const downloadURI = | |
typeof URL.createObjectURL === 'undefined' | |
? dataURI | |
: URL.createObjectURL(blob); | |
let link = document.createElement('a'); | |
link.setAttribute('href', downloadURI); | |
link.setAttribute('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