refactor(base64): split base64 text and file conversion in two tools + base64 to file

This commit is contained in:
Corentin Thomasset 2022-07-25 23:21:42 +02:00
parent a70a0f83a1
commit e6953d1b67
No known key found for this signature in database
GPG key ID: DBD997E935996158
8 changed files with 117 additions and 21 deletions

View file

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