Skip to content

Instantly share code, notes, and snippets.

@Foovanadil
Last active August 12, 2024 21:57
Show Gist options
  • Save Foovanadil/fb2f0d4bd53144c03c5be6897e2268b7 to your computer and use it in GitHub Desktop.
Save Foovanadil/fb2f0d4bd53144c03c5be6897e2268b7 to your computer and use it in GitHub Desktop.
JS Clientside download file from blob
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