Files
UT-Registration-Plus/src/shared/util/downloadBlob.ts
Samuel Gunter 7d4c5d7be8 feat: screenshot whole page, hide certain elements, screenshot fixed size (#180)
* feat: screenshot whole page, hide certain elements, screenshot fixed size

* refactor: use variants instead of groups and custom rules

* feat: scaled header, smaller body, weird padding/margin changes

* feat: consistent sizing & style regardless of zoom

* feat: use downloadBlob instead of hand-rolled image saving

* fix: be type safe is toBlob returns Promise<null>

* fix: revoke object url when it should be

* fix: animation scheduling

---------

Co-authored-by: Razboy20 <razboy20@gmail.com>
2024-03-21 19:20:03 -05:00

31 lines
1.1 KiB
TypeScript

import type { MIMETypeKey } from '../types/MIMEType';
import MIMEType from '../types/MIMEType';
/**
* Downloads a blob by creating a temporary URL and triggering a download.
* @param blobPart The blob data to be downloaded.
* @param type The MIME type of the blob.
* @param fileName The name of the file to be downloaded.
* @returns A promise that resolves when the download is successful, or rejects with an error if the download fails.
*/
export function downloadBlob(blobPart: BlobPart, type: MIMETypeKey, fileName: string): Promise<void> {
return new Promise((resolve, reject) => {
const blob: Blob = new Blob([blobPart], { type: MIMEType[type] });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = fileName;
link.addEventListener('click', () => {
resolve();
});
link.addEventListener('error', () => {
URL.revokeObjectURL(url);
reject(new Error('Download failed'));
});
link.click();
URL.revokeObjectURL(url);
});
}