Skip to content

Instantly share code, notes, and snippets.

@FloStar3000
Created June 11, 2021 07:45
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 FloStar3000/658970df5baa42dbd63fcc26fd98a26c to your computer and use it in GitHub Desktop.
Save FloStar3000/658970df5baa42dbd63fcc26fd98a26c to your computer and use it in GitHub Desktop.
Download File programmatically on website / JS/TS
const download = async () => {
const fetchResult = await fetch("http://mywebsite.com/download")
if(fetchResult.status === 200){
const blob = await fetchResult.blob();
var url = window.URL.createObjectURL(blob)
var a = document.createElement("a")
a.href = url
a.download = `DownloadFilename.csv`
document.body.appendChild(a) // we need to append the element to the dom -> otherwise it will not work in firefox
a.click()
a.remove() //afterwards we remove the element again
}
else{
const json = await fetchResult.json();
alert(json.message)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment