Skip to content

Instantly share code, notes, and snippets.

@hellomoto177
Last active July 16, 2021 07:40
Show Gist options
  • Save hellomoto177/d94abe89a6646362ede329262082dcec to your computer and use it in GitHub Desktop.
Save hellomoto177/d94abe89a6646362ede329262082dcec to your computer and use it in GitHub Desktop.
Download blob
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