Files
web/app/components/media/FileUpload.vue
2026-05-07 10:53:59 +07:00

122 lines
2.9 KiB
Vue

<template>
<div>
<div
id="ignore"
class="file is-primary"
>
<label
class="file-label"
v-bind="$attrs"
>
<input
class="file-input"
type="file"
:id="docid"
multiple
name="resume"
@change="doChange"
/>
<span class="file-cta px-3 py-1.5">
<span class="icon-text is-gap-0.5 is-align-items-center">
<slot name="icon">
<Icon
name="material-symbols:attach-file-rounded"
:size="20"
/>
</slot>
<slot />
</span>
</span>
</label>
</div>
<Modal
v-bind="showmodal"
@close="showmodal = undefined"
@files="getFiles"
/>
</div>
</template>
<script setup>
defineOptions({
inheritAttrs: false,
});
const props = defineProps({
type: Array,
convert: String,
quality: Number,
});
const emit = defineEmits(["files"]);
const { $id, $snackbar, $upload } = useNuxtApp();
const vtype = props.type || ["image"];
const files = ref();
const dataFiles = ref();
const showmodal = ref();
const docid = $id();
function getFileExtension(fileName) {
if (!fileName || typeof fileName !== "string") return "";
const parts = fileName.split(".");
return parts.length > 1 ? parts.pop().toLowerCase() : "";
}
function getType(ext) {
// copied from 01-common.js
const imageFormat = ["png", "jpg", "jpeg", "bmp", "gif", "svg", "webp"];
const videoFormat = ["wmv", "avi", "mp4", "flv", "mov", "mpg", "amv", "rm"];
if (ext === "pdf") return "pdf";
if (imageFormat.includes(ext)) return "image";
if (videoFormat.includes(ext)) return "video";
return "file";
}
function doChange() {
dataFiles.value = [];
const fileList = document.getElementById(docid).files;
files.value = Array.from(fileList);
if (files.value.length === 0) return;
// Xác định giá trị convert: "1" nếu convert được bật, "0" nếu không
const convertValue = props.convert ? "1" : "0";
const qualityValue = props.convert && props.quality ? props.quality : null;
files.value.map((v) => {
const ext = getFileExtension(v.name);
const type = getType(ext);
if (!vtype.includes(type)) {
$snackbar(`Định dạng file "${ext}" không hợp lệ`);
return;
}
const file = $upload(v, type, 1, convertValue, qualityValue);
dataFiles.value.push(file);
});
showmodal.value = {
component: "media/UploadProgress",
title: "Upload files",
width: "700px",
height: "200px",
vbind: { files: dataFiles.value },
};
clearFileList();
}
function clearFileList() {
const fileInput = document.getElementById(docid);
const dt = new DataTransfer();
fileInput.files = dt.files;
}
function getFiles(files) {
emit("files", files);
setTimeout(() => {
showmodal.value = undefined;
}, 3000);
}
</script>