diff options
author | 2022-07-25 23:21:42 +0200 | |
---|---|---|
committer | 2022-07-25 23:21:42 +0200 | |
commit | e6953d1b67b81a6d3c19973b706f29637c421f98 (patch) | |
tree | b4a2717bb5e8cb05fa0955fb53a1558f68678e63 /src/composable | |
parent | a70a0f83a1c500247194182f33be40526c0f0def (diff) | |
download | it-tools-e6953d1b67b81a6d3c19973b706f29637c421f98.tar.gz it-tools-e6953d1b67b81a6d3c19973b706f29637c421f98.tar.zst it-tools-e6953d1b67b81a6d3c19973b706f29637c421f98.zip |
refactor(base64): split base64 text and file conversion in two tools + base64 to file
Diffstat (limited to 'src/composable')
-rw-r--r-- | src/composable/downloadBase64.ts | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/src/composable/downloadBase64.ts b/src/composable/downloadBase64.ts index e5b621f..d2ea576 100644 --- a/src/composable/downloadBase64.ts +++ b/src/composable/downloadBase64.ts @@ -1,11 +1,17 @@ +import { extension as getExtensionFromMime } from 'mime-types'; import type { Ref } from 'vue'; -export function useDownloadFileFromBase64({ source, filename = 'file' }: { source: Ref<string>; filename?: string }) { +export function useDownloadFileFromBase64({ source, filename }: { source: Ref<string>; filename?: string }) { return { download() { + const base64 = source.value; + const mimeType = base64.match(/data:(.*?);base64/i)?.[1] ?? 'text/plain'; + console.log({ mimeType }); + const cleanFileName = filename ?? `file.${getExtensionFromMime(mimeType)}`; + const a = document.createElement('a'); a.href = source.value; - a.download = filename; + a.download = cleanFileName; a.click(); }, }; |