-
-
Save FloStar3000/658970df5baa42dbd63fcc26fd98a26c to your computer and use it in GitHub Desktop.
Download File programmatically on website / JS/TS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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