mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-05 13:57:10 -04:00
feat(base64 file converter): add a preview image
Fix #594. Taken from #595 (thanks @SAF2k)
This commit is contained in:
parent
435d81281b
commit
a25bd8f4b0
2 changed files with 52 additions and 2 deletions
|
@ -6,6 +6,7 @@ export {
|
||||||
getMimeTypeFromBase64,
|
getMimeTypeFromBase64,
|
||||||
getMimeTypeFromExtension, getExtensionFromMimeType,
|
getMimeTypeFromExtension, getExtensionFromMimeType,
|
||||||
useDownloadFileFromBase64, useDownloadFileFromBase64Refs,
|
useDownloadFileFromBase64, useDownloadFileFromBase64Refs,
|
||||||
|
previewImageFromBase64,
|
||||||
};
|
};
|
||||||
|
|
||||||
const commonMimeTypesSignatures = {
|
const commonMimeTypesSignatures = {
|
||||||
|
@ -92,3 +93,26 @@ function useDownloadFileFromBase64Refs(
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function previewImageFromBase64(base64String: string): HTMLImageElement {
|
||||||
|
if (base64String === '') {
|
||||||
|
throw new Error('Base64 string is empty');
|
||||||
|
}
|
||||||
|
|
||||||
|
const img = document.createElement('img');
|
||||||
|
img.src = base64String;
|
||||||
|
|
||||||
|
const container = document.createElement('div');
|
||||||
|
container.appendChild(img);
|
||||||
|
|
||||||
|
const previewContainer = document.getElementById('previewContainer');
|
||||||
|
if (previewContainer) {
|
||||||
|
previewContainer.innerHTML = '';
|
||||||
|
previewContainer.appendChild(container);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new Error('Preview container element not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
return img;
|
||||||
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
import { useBase64 } from '@vueuse/core';
|
import { useBase64 } from '@vueuse/core';
|
||||||
import type { Ref } from 'vue';
|
import type { Ref } from 'vue';
|
||||||
import { useCopy } from '@/composable/copy';
|
import { useCopy } from '@/composable/copy';
|
||||||
import { getExtensionFromMimeType, getMimeTypeFromBase64, useDownloadFileFromBase64Refs } from '@/composable/downloadBase64';
|
import { getExtensionFromMimeType, getMimeTypeFromBase64, previewImageFromBase64, useDownloadFileFromBase64Refs } from '@/composable/downloadBase64';
|
||||||
import { useValidation } from '@/composable/validation';
|
import { useValidation } from '@/composable/validation';
|
||||||
import { isValidBase64 } from '@/utils/base64';
|
import { isValidBase64 } from '@/utils/base64';
|
||||||
|
|
||||||
|
@ -35,6 +35,25 @@ watch(
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
function previewImage() {
|
||||||
|
if (!base64InputValidation.isValid) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const image = previewImageFromBase64(base64Input.value);
|
||||||
|
image.style.maxWidth = '100%';
|
||||||
|
image.style.maxHeight = '400px';
|
||||||
|
const previewContainer = document.getElementById('previewContainer');
|
||||||
|
if (previewContainer) {
|
||||||
|
previewContainer.innerHTML = '';
|
||||||
|
previewContainer.appendChild(image);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (_) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function downloadFile() {
|
function downloadFile() {
|
||||||
if (!base64InputValidation.isValid) {
|
if (!base64InputValidation.isValid) {
|
||||||
return;
|
return;
|
||||||
|
@ -88,7 +107,14 @@ async function onUpload(file: File) {
|
||||||
mb-2
|
mb-2
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div flex justify-center>
|
<div flex justify-center py-2>
|
||||||
|
<div id="previewContainer" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div flex justify-center gap-3>
|
||||||
|
<c-button :disabled="base64Input === '' || !base64InputValidation.isValid" @click="previewImage()">
|
||||||
|
Preview image
|
||||||
|
</c-button>
|
||||||
<c-button :disabled="base64Input === '' || !base64InputValidation.isValid" @click="downloadFile()">
|
<c-button :disabled="base64Input === '' || !base64InputValidation.isValid" @click="downloadFile()">
|
||||||
Download file
|
Download file
|
||||||
</c-button>
|
</c-button>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue