Skip to content

Instantly share code, notes, and snippets.

@suissa
Forked from fabiobusnello/download.js
Created June 14, 2018 16:59
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 suissa/e54642f839fcfd4295888158f2312ded to your computer and use it in GitHub Desktop.
Save suissa/e54642f839fcfd4295888158f2312ded 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