Skip to content

Instantly share code, notes, and snippets.

@fabiobusnello
Last active June 14, 2018 16:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fabiobusnello/e43f2d41db971f997fa19727d561ec24 to your computer and use it in GitHub Desktop.
Save fabiobusnello/e43f2d41db971f997fa19727d561ec24 to your computer and use it in GitHub Desktop.
downloads using fetch
export const download = async (params = {}) => {
const caching = await fetch(params.url, params)
const fileName = caching.headers.get('Content-Disposition')
const ext = fileName.split('.')[fileName.split('.').length - 1].replace('"', '')
const blob = await caching.blob()
const url = await URL.createObjectURL(blob)
const a = document.createElement("a");
a.href = url
a.download = `Reports.${ext}`
a.click()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment