2022-07-25 23:21:42 +02:00
|
|
|
import { extension as getExtensionFromMime } from 'mime-types';
|
2022-04-14 18:18:15 +02:00
|
|
|
import type { Ref } from 'vue';
|
|
|
|
|
2022-07-25 23:21:42 +02:00
|
|
|
export function useDownloadFileFromBase64({ source, filename }: { source: Ref<string>; filename?: string }) {
|
2022-04-14 18:18:15 +02:00
|
|
|
return {
|
|
|
|
download() {
|
2022-07-25 23:21:42 +02:00
|
|
|
const base64 = source.value;
|
|
|
|
const mimeType = base64.match(/data:(.*?);base64/i)?.[1] ?? 'text/plain';
|
|
|
|
console.log({ mimeType });
|
|
|
|
const cleanFileName = filename ?? `file.${getExtensionFromMime(mimeType)}`;
|
|
|
|
|
2022-04-14 18:18:15 +02:00
|
|
|
const a = document.createElement('a');
|
|
|
|
a.href = source.value;
|
2022-07-25 23:21:42 +02:00
|
|
|
a.download = cleanFileName;
|
2022-04-14 18:18:15 +02:00
|
|
|
a.click();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|