게시판 저장 수정정,파일제한 추가

This commit is contained in:
dyhj625 2025-01-24 14:44:18 +09:00
parent 2f0f5fd378
commit fb12e345a7
3 changed files with 48 additions and 20 deletions

View File

@ -1,17 +1,25 @@
<template> <template>
<div class="mb-4 row"> <div class="mb-4 row">
<label :for="name" class="col-md-2 col-form-label">{{ title }} </label> <label :for="name" class="col-md-2 col-form-label">{{ title }}</label>
<div class="col-md-10"> <div class="col-md-10">
<input class="form-control" type="file" :id="name" @change="changeHandler" multiple /> <input
class="form-control"
type="file"
:id="name"
@change="changeHandler"
multiple
/>
<div v-if="showError" class="text-danger mt-1">
{{ errorMsg }}
</div>
</div> </div>
<div class="invalid-feedback" :class="isAlert ? 'display-block' : ''">{{ errorMsg }}</div>
</div> </div>
</template> </template>
<script setup> <script setup>
import { fileMsg } from '@/common/msgEnum'; import { ref, watch } from 'vue';
import { ref } from 'vue';
// Props
const prop = defineProps({ const prop = defineProps({
title: { title: {
type: String, type: String,
@ -23,24 +31,34 @@ const prop = defineProps({
default: 'nameplz', default: 'nameplz',
required: true, required: true,
}, },
isAlert : { isAlert: {
type: Boolean, type: Boolean,
default: false, default: false,
required: false, required: false,
} },
}); });
const emits = defineEmits(['update:data']); // Emits
const errorMsg = ref(fileMsg.FileMaxSizeMsg); const emits = defineEmits(['update:data', 'update:isValid']);
// ...
// Constants
const MAX_TOTAL_SIZE = 5 * 1024 * 1024; // 5MB
const errorMsg = ref('첨부파일의 총 용량이 5MB를 초과합니다.');
const showError = ref(false);
// Change Handler
const changeHandler = (event) => { const changeHandler = (event) => {
const files = Array.from(event.target.files); const files = Array.from(event.target.files);
emits('update:data', files); const totalSize = files.reduce((sum, file) => sum + file.size, 0);
if (totalSize > MAX_TOTAL_SIZE) {
showError.value = true; //
emits('update:data', []); //
emits('update:isValid', false); //
} else {
showError.value = false; //
emits('update:data', files); //
emits('update:isValid', true); //
}
}; };
</script> </script>
<style>
</style>

View File

@ -2,7 +2,7 @@
<div class="mb-2 row"> <div class="mb-2 row">
<label :for="name" class="col-md-2 col-form-label" :class="isLabel ? 'd-block' : 'd-none'"> <label :for="name" class="col-md-2 col-form-label" :class="isLabel ? 'd-block' : 'd-none'">
{{ title }} {{ title }}
<span :class="isEssential ? 'text-red' : 'none'">*</span> <span v-if="isEssential" class="text-danger">*</span>
</label> </label>
<div class="col-md-10"> <div class="col-md-10">
<input <input

View File

@ -57,6 +57,7 @@
name="files" name="files"
:is-alert="attachFilesAlert" :is-alert="attachFilesAlert"
@update:data="attachFiles = $event" @update:data="attachFiles = $event"
@update:isValid="isFileValid = $event"
/> />
<div class="mb-4"> <div class="mb-4">
@ -71,8 +72,17 @@
</div> </div>
<div class="mb-4 d-flex justify-content-end"> <div class="mb-4 d-flex justify-content-end">
<button type="button" class="btn btn-info" @click="goList"><i class='bx bx-left-arrow-alt'></i></button> <button type="button" class="btn btn-info" @click="goList">
<button type="button" class="btn btn-primary ms-1" @click="write"><i class='bx bx-check'></i></button> <i class="bx bx-left-arrow-alt"></i>
</button>
<button
type="button"
class="btn btn-primary ms-1"
@click="write"
:disabled="!isFileValid"
>
<i class="bx bx-check"></i>
</button>
</div> </div>
</div> </div>
</div> </div>
@ -94,6 +104,7 @@ const password = ref('');
const category = ref(0); // 0 const category = ref(0); // 0
const content = ref(''); const content = ref('');
const attachFiles = ref(null); const attachFiles = ref(null);
const isFileValid = ref(true); //
const titleAlert = ref(false); const titleAlert = ref(false);
const passwordAlert = ref(false); const passwordAlert = ref(false);
@ -112,7 +123,7 @@ const write = async () => {
passwordAlert.value = category.value === 1 && !password.value; passwordAlert.value = category.value === 1 && !password.value;
contentAlert.value = !content.value; contentAlert.value = !content.value;
if (titleAlert.value || passwordAlert.value || contentAlert.value) { if (titleAlert.value || passwordAlert.value || contentAlert.value || !isFileValid.value) {
return; return;
} }
@ -125,7 +136,6 @@ const write = async () => {
}; };
const { data: boardResponse } = await axios.post('board', boardData); const { data: boardResponse } = await axios.post('board', boardData);
console.log(boardResponse.data);
const boardId = boardResponse.data; const boardId = boardResponse.data;
if (attachFiles.value && attachFiles.value.length > 0) { if (attachFiles.value && attachFiles.value.length > 0) {