1
0
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:
Juan Picado @jotadeveloper
2019-07-28 14:12:18 +02:00
parent 83b6a9d247
commit f47ab2490b
2 changed files with 21 additions and 2 deletions

View File

@@ -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);
}