Last active
October 21, 2022 13:33
-
-
Save Karytonn/a8b2590c065c08e884e967860d95c460 to your computer and use it in GitHub Desktop.
Trigger to download external file
This file contains 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
downloadFile(url: string) { | |
fetch(url).then(res => res.blob()).then(file => { | |
let tempUrl = URL.createObjectURL(file); | |
const triggerToDownload = document.createElement("a"); | |
triggerToDownload.href = tempUrl; | |
triggerToDownload.download = url.replace(/^.*[\\\/]/, ''); | |
document.body.appendChild(triggerToDownload); | |
triggerToDownload.click(); | |
URL.revokeObjectURL(tempUrl); | |
triggerToDownload.remove(); | |
}).catch(() => { | |
alert("Houve uma falha ao fazer download do arquivo, tente novamente mais tarde."); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment