Skip to content

Instantly share code, notes, and snippets.

@Neugierdsnase
Created May 19, 2020 15:05
Show Gist options
  • Save Neugierdsnase/9bca6e01564f0df78fc08a95a279fc72 to your computer and use it in GitHub Desktop.
Save Neugierdsnase/9bca6e01564f0df78fc08a95a279fc72 to your computer and use it in GitHub Desktop.
directly download a file from a server in the browser
export const downloadFile = (data: string, type: string, fileName: string) => {
const url = window.URL.createObjectURL(new Blob([data]))
const a = document.createElement('a')
a.href = url
switch (type) {
case 'text/csv':
default:
a.download = `${fileName}.csv`
}
a.click()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment