Last active
July 16, 2021 07:40
-
-
Save hellomoto177/d94abe89a6646362ede329262082dcec to your computer and use it in GitHub Desktop.
Download blob
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
export const downloadBlob = (blob: Blob, filename?: string) => { | |
if (!blob) return; | |
const blobURL = window.URL.createObjectURL(blob); | |
if (filename) { | |
const a = document.createElement('a'); | |
if (blob.type !== 'application/pdf') { | |
a.download = filename; | |
} | |
a.href = blobURL; | |
a.target = '_blank'; | |
document.body.appendChild(a); | |
a.click(); | |
return document.body.removeChild(a); | |
} | |
window.open(blobURL); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment