Skip to content

Instantly share code, notes, and snippets.

@AwesomeZaidi
Created December 1, 2022 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AwesomeZaidi/139f2ec2add8c8a30cd182c3f5a031b2 to your computer and use it in GitHub Desktop.
Save AwesomeZaidi/139f2ec2add8c8a30cd182c3f5a031b2 to your computer and use it in GitHub Desktop.
downloadCsv
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