fix: 修复后台文件上传无反馈

This commit is contained in:
Nixevol
2026-07-04 07:22:16 +08:00
parent f51f45bcb1
commit 0f076dff45
2 changed files with 163 additions and 45 deletions
+161 -45
View File
@@ -1,29 +1,47 @@
<template>
<n-upload
:list-type="listType"
:file-list="fileList"
:max="max"
:accept="accept"
:custom-request="doUpload"
@remove="onRemove"
>
<template v-if="listType === 'image-card'">
<div class="upd-trigger">
<div class="media-upload" :class="{ text: listType === 'text' }">
<div v-if="listType === 'image-card'" class="image-grid">
<div v-for="m in items" :key="m.key" class="image-item">
<img v-if="isImage(m)" class="image-thumb" :src="m.url || m.key" :alt="m.name || m.key" />
<div v-else class="file-thumb">{{ fileExt(m) }}</div>
<button class="remove" type="button" :title="t('common.action.delete')" @click="remove(m.key)">×</button>
</div>
<div v-for="u in uploading" :key="u.id" class="image-item uploading">
<n-spin size="small" />
</div>
<label v-if="canUpload" class="image-item add">
<input type="file" :accept="accept" :multiple="allowMultiple" hidden @change="onPick" />
<n-icon :component="AddOutline" :size="22" />
<span>{{ t('common.action.upload') }}</span>
</label>
</div>
<div v-else class="file-list">
<div v-for="m in items" :key="m.key" class="file-row">
<span class="file-name" :title="m.name || m.key">{{ m.name || m.key }}</span>
<n-button quaternary circle size="tiny" type="error" :title="t('common.action.delete')" @click="remove(m.key)">
×
</n-button>
</div>
</template>
<n-button v-else>
<template #icon><n-icon :component="CloudUploadOutline" /></template>
{{ t('common.action.upload') }}
</n-button>
</n-upload>
<div v-for="u in uploading" :key="u.id" class="file-row uploading-row">
<n-spin size="small" />
<span>{{ t('common.action.upload') }}...</span>
</div>
<label v-if="canUpload" class="file-button">
<input type="file" :accept="accept" :multiple="allowMultiple" hidden @change="onPick" />
<n-button tag="span">
<template #icon><n-icon :component="CloudUploadOutline" /></template>
{{ t('common.action.upload') }}
</n-button>
</label>
</div>
</div>
</template>
<script setup lang="ts">
import { computed } from 'vue';
import { computed, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import type { UploadCustomRequestOptions, UploadFileInfo } from 'naive-ui';
import { NButton, NIcon, NSpin } from 'naive-ui';
import { AddOutline, CloudUploadOutline } from '@vicons/ionicons5';
import { post } from '@/api/request';
import { message } from '@/utils/feedback';
@@ -42,50 +60,148 @@ const props = withDefaults(
const emit = defineEmits<{ (e: 'update:modelValue', v: MediaItem[]): void }>();
const { t } = useI18n();
const uploading = ref<{ id: number }[]>([]);
let seq = 0;
const items = computed(() => props.modelValue || []);
const listType = computed(() => (props.type === 'image' ? 'image-card' : 'text'));
const accept = computed(() =>
props.type === 'image' ? 'image/*' : props.type === 'video' ? 'video/*' : 'application/pdf,image/*',
);
const allowMultiple = computed(() => props.max > 1);
const canUpload = computed(() => items.value.length + uploading.value.length < props.max);
const fileList = computed<UploadFileInfo[]>(() =>
(props.modelValue || []).map((m) => ({
id: m.key,
name: m.name || m.key,
status: 'finished',
url: m.url,
})),
);
function isImage(m: MediaItem) {
return props.type === 'image' || /^image\//.test(m.type || '') || /\.(png|jpe?g|gif|webp|bmp|svg)$/i.test(m.url || m.key);
}
async function doUpload({ file, onFinish, onError }: UploadCustomRequestOptions) {
try {
const fd = new FormData();
fd.append('file', file.file as File);
const res: any = await post(`/upload?type=${props.type}`, fd);
emit('update:modelValue', [
...(props.modelValue || []),
{ key: res.key, url: res.url, name: res.name, type: props.type },
]);
onFinish();
} catch (e) {
message.error((e as Error)?.message || 'upload failed');
onError();
function fileExt(m: MediaItem) {
const name = m.name || m.key;
const ext = name.split('.').pop();
return ext && ext.length <= 5 ? ext.toUpperCase() : 'FILE';
}
async function onPick(e: Event) {
const input = e.target as HTMLInputElement;
const files = Array.from(input.files || []);
input.value = '';
for (const file of files) {
if (items.value.length + uploading.value.length >= props.max) {
message.warning(t('common.msg.maxReached', { n: props.max }));
break;
}
await uploadOne(file);
}
}
function onRemove({ file }: { file: UploadFileInfo }) {
emit('update:modelValue', (props.modelValue || []).filter((m) => m.key !== file.id));
return true;
async function uploadOne(file: File) {
const holder = { id: ++seq };
uploading.value.push(holder);
try {
const fd = new FormData();
fd.append('file', file);
const res: any = await post(`/upload?type=${props.type}`, fd);
emit('update:modelValue', [
...items.value,
{ key: res.key, url: res.url, name: res.name || file.name, type: props.type },
]);
} catch (err: any) {
message.error(err?.message || 'upload failed');
} finally {
uploading.value = uploading.value.filter((u) => u.id !== holder.id);
}
}
function remove(key: string) {
emit('update:modelValue', items.value.filter((m) => m.key !== key));
}
</script>
<style scoped>
.upd-trigger {
.media-upload {
width: 100%;
}
.image-grid {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.image-item {
position: relative;
width: 96px;
height: 96px;
border-radius: 8px;
overflow: hidden;
border: 1px solid var(--n-border-color, #e0e0e6);
background: #f4f7fc;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 4px;
}
.image-thumb {
width: 100%;
height: 100%;
object-fit: cover;
}
.file-thumb {
font-size: 12px;
color: #6b7280;
}
.image-item.add,
.image-item.uploading {
flex-direction: column;
gap: 4px;
font-size: 12px;
color: #6b7280;
cursor: pointer;
border-style: dashed;
background: #fafcff;
}
.image-item.uploading {
cursor: default;
}
.remove {
position: absolute;
top: 2px;
right: 2px;
width: 20px;
height: 20px;
border: none;
border-radius: 50%;
background: rgba(0, 0, 0, 0.55);
color: #fff;
font-size: 14px;
line-height: 20px;
cursor: pointer;
padding: 0;
}
.remove:hover {
background: #d03050;
}
.file-list {
display: flex;
flex-direction: column;
gap: 8px;
align-items: flex-start;
}
.file-row {
display: inline-flex;
align-items: center;
gap: 8px;
max-width: 100%;
min-height: 28px;
}
.file-name {
max-width: 360px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.uploading-row {
color: #6b7280;
font-size: 13px;
}
.file-button {
display: inline-flex;
}
</style>