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

89 lines
2.5 KiB
Vue
Raw Normal View History

2022-04-14 02:30:25 +02:00
<template>
<c-card title="Base64 to file">
<n-form-item
:feedback="base64InputValidation.message"
:validation-status="base64InputValidation.status"
:show-label="false"
>
<n-input v-model:value="base64Input" type="textarea" placeholder="Put your base64 file string here..." rows="5" />
</n-form-item>
2022-04-14 02:30:25 +02:00
<n-space justify="center">
<c-button :disabled="base64Input === '' || !base64InputValidation.isValid" @click="downloadFile()">
Download file
</c-button>
2022-04-14 02:30:25 +02:00
</n-space>
</c-card>
2022-04-14 02:30:25 +02:00
<c-card title="File to base64">
2022-04-22 23:31:40 +02:00
<n-upload v-model:file-list="fileList" :show-file-list="true" :on-before-upload="onUpload" list-type="image">
2022-04-14 02:30:25 +02:00
<n-upload-dragger>
<div mb-2>
2022-04-22 23:31:40 +02:00
<n-icon size="35" :depth="3" :component="Upload" />
2022-04-14 02:30:25 +02:00
</div>
2022-04-22 23:31:40 +02:00
<n-text style="font-size: 14px"> Click or drag a file to this area to upload </n-text>
2022-04-14 02:30:25 +02:00
</n-upload-dragger>
</n-upload>
2022-07-25 23:23:53 +02:00
<n-input :value="fileBase64" type="textarea" readonly placeholder="File in base64 will be here" />
2022-04-14 02:30:25 +02:00
<n-space justify="center">
<c-button @click="copyFileBase64()"> Copy </c-button>
2022-04-14 02:30:25 +02:00
</n-space>
</c-card>
2022-04-14 02:30:25 +02:00
</template>
<script setup lang="ts">
2022-04-22 23:31:40 +02:00
import { useCopy } from '@/composable/copy';
import { useDownloadFileFromBase64 } from '@/composable/downloadBase64';
import { useValidation } from '@/composable/validation';
import { isValidBase64 } from '@/utils/base64';
2022-04-22 23:31:40 +02:00
import { Upload } from '@vicons/tabler';
import { useBase64 } from '@vueuse/core';
2022-04-14 02:30:25 +02:00
import type { UploadFileInfo } from 'naive-ui';
import { ref, type Ref } from 'vue';
2022-04-14 02:30:25 +02:00
const base64Input = ref('');
const { download } = useDownloadFileFromBase64({ source: base64Input });
const base64InputValidation = useValidation({
source: base64Input,
rules: [
{
message: 'Invalid base 64 string',
validator: (value) => isValidBase64(value.trim()),
},
],
});
function downloadFile() {
if (!base64InputValidation.isValid) return;
try {
download();
} catch (_) {
//
}
}
2022-04-14 02:30:25 +02:00
2022-04-22 23:31:40 +02:00
const fileList = ref();
const fileInput = ref() as Ref<File>;
const { base64: fileBase64 } = useBase64(fileInput);
const { copy: copyFileBase64 } = useCopy({ source: fileBase64, text: 'Base64 string copied to the clipboard' });
2022-04-14 02:30:25 +02:00
async function onUpload({ file: { file } }: { file: UploadFileInfo }) {
2022-04-14 02:30:25 +02:00
if (file) {
2022-04-22 23:31:40 +02:00
fileList.value = [];
fileInput.value = file;
2022-04-14 02:30:25 +02:00
}
}
</script>
<style lang="less" scoped>
.n-input,
.n-upload {
2022-04-14 02:30:25 +02:00
margin-bottom: 15px;
}
::v-deep(.n-upload-trigger) {
width: 100%;
}
2022-04-22 23:31:40 +02:00
</style>