Skip to content

Instantly share code, notes, and snippets.

@Karytonn
Last active October 21, 2022 13:33
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 Karytonn/a8b2590c065c08e884e967860d95c460 to your computer and use it in GitHub Desktop.
Save Karytonn/a8b2590c065c08e884e967860d95c460 to your computer and use it in GitHub Desktop.
Trigger to download external file
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