Skip to content

Commit a25bd8f

Browse files
committed
feat(base64 file converter): add a preview image
Fix CorentinTh#594. Taken from CorentinTh#595 (thanks @SAF2k)
1 parent 435d812 commit a25bd8f

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/composable/downloadBase64.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export {
66
getMimeTypeFromBase64,
77
getMimeTypeFromExtension, getExtensionFromMimeType,
88
useDownloadFileFromBase64, useDownloadFileFromBase64Refs,
9+
previewImageFromBase64,
910
};
1011

1112
const commonMimeTypesSignatures = {
@@ -92,3 +93,26 @@ function useDownloadFileFromBase64Refs(
9293
},
9394
};
9495
}
96+
97+
function previewImageFromBase64(base64String: string): HTMLImageElement {
98+
if (base64String === '') {
99+
throw new Error('Base64 string is empty');
100+
}
101+
102+
const img = document.createElement('img');
103+
img.src = base64String;
104+
105+
const container = document.createElement('div');
106+
container.appendChild(img);
107+
108+
const previewContainer = document.getElementById('previewContainer');
109+
if (previewContainer) {
110+
previewContainer.innerHTML = '';
111+
previewContainer.appendChild(container);
112+
}
113+
else {
114+
throw new Error('Preview container element not found');
115+
}
116+
117+
return img;
118+
}

src/tools/base64-file-converter/base64-file-converter.vue

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { useBase64 } from '@vueuse/core';
33
import type { Ref } from 'vue';
44
import { useCopy } from '@/composable/copy';
5-
import { getExtensionFromMimeType, getMimeTypeFromBase64, useDownloadFileFromBase64Refs } from '@/composable/downloadBase64';
5+
import { getExtensionFromMimeType, getMimeTypeFromBase64, previewImageFromBase64, useDownloadFileFromBase64Refs } from '@/composable/downloadBase64';
66
import { useValidation } from '@/composable/validation';
77
import { isValidBase64 } from '@/utils/base64';
88
@@ -35,6 +35,25 @@ watch(
3535
},
3636
);
3737
38+
function previewImage() {
39+
if (!base64InputValidation.isValid) {
40+
return;
41+
}
42+
try {
43+
const image = previewImageFromBase64(base64Input.value);
44+
image.style.maxWidth = '100%';
45+
image.style.maxHeight = '400px';
46+
const previewContainer = document.getElementById('previewContainer');
47+
if (previewContainer) {
48+
previewContainer.innerHTML = '';
49+
previewContainer.appendChild(image);
50+
}
51+
}
52+
catch (_) {
53+
//
54+
}
55+
}
56+
3857
function downloadFile() {
3958
if (!base64InputValidation.isValid) {
4059
return;
@@ -88,7 +107,14 @@ async function onUpload(file: File) {
88107
mb-2
89108
/>
90109

91-
<div flex justify-center>
110+
<div flex justify-center py-2>
111+
<div id="previewContainer" />
112+
</div>
113+
114+
<div flex justify-center gap-3>
115+
<c-button :disabled="base64Input === '' || !base64InputValidation.isValid" @click="previewImage()">
116+
Preview image
117+
</c-button>
92118
<c-button :disabled="base64Input === '' || !base64InputValidation.isValid" @click="downloadFile()">
93119
Download file
94120
</c-button>

0 commit comments

Comments
 (0)