Last active
August 12, 2024 21:57
-
-
Save Foovanadil/fb2f0d4bd53144c03c5be6897e2268b7 to your computer and use it in GitHub Desktop.
JS Clientside download file from blob
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 clientsideDownloadFile = (blob,fileName) => { | |
// Test download attribute first | |
if ('download' in HTMLAnchorElement.prototype) { | |
// Create an object URL for the blob object | |
const url = URL.createObjectURL(blob); | |
// Create a new anchor element | |
const a = document.createElement('a'); | |
a.href = url; | |
a.download = fileName; | |
// Programmatically trigger a click on the anchor element | |
// Useful if you want the download to happen automatically | |
// Without attaching the anchor element to the DOM | |
a.click(); | |
setTimeout(() => { | |
URL.revokeObjectURL(url); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment