mirror of
https://github.com/SomboChea/ui
synced 2026-01-18 09:06:14 +07:00
refactor: add download file method
This commit is contained in:
@@ -18,3 +18,20 @@ export function getRegistryURL(): string {
|
||||
// Don't add slash if it's not a sub directory
|
||||
return `${location.origin}${location.pathname === '/' ? '' : location.pathname}`;
|
||||
}
|
||||
|
||||
export function extractFileName(url: string): string {
|
||||
return url.substring(url.lastIndexOf('/') + 1);
|
||||
}
|
||||
|
||||
export function downloadFile(fileStream: Blob, fileName: string): void {
|
||||
const file = new File([fileStream], fileName, { type: 'application/octet-stream', lastModified: Date.now() });
|
||||
const objectURL = URL.createObjectURL(file);
|
||||
const fileLink = document.createElement('a');
|
||||
fileLink.href = objectURL;
|
||||
fileLink.download = fileName;
|
||||
fileLink.click();
|
||||
// firefox requires remove the object url
|
||||
setTimeout(() => {
|
||||
URL.revokeObjectURL(objectURL);
|
||||
}, 150);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user