支持拖拽和点击上传的文件上传组件,带进度显示和文件管理功能。
<script setup lang="ts">
const fileList = ref([])
</script>
<template>
<FaFileUpload
v-model="fileList"
action="/api/upload"
/>
</template>| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
action | string | 必需 | 上传接口地址 |
method | string | 'post' | 请求方法 |
headers | Headers | Record<string, any> | {} | 请求头 |
data | Record<string, any> | {} | 额外表单数据 |
name | string | 'file' | 文件字段名 |
afterUpload | (response: any) => string | Promise<string> | - | 上传成功后的处理函数,返回文件 URL |
multiple | boolean | false | 是否支持多选 |
ext | string[] | [] | 允许的文件扩展名 |
size | number | 10485760 (10MB) | 单个文件大小限制(字节) |
max | number | 0 (无限制) | 最大上传数量 |
hideTips | boolean | false | 是否隐藏提示信息 |
disabled | boolean | false | 是否禁用 |
| 名称 | 类型 | 说明 |
|---|---|---|
modelValue | FileItem[] | 已上传的文件列表(必需) |
interface FileItem {
name: string
size: number
url?: string
status?: 'uploading' | 'success' | 'error'
progress?: number
file?: File
}| 事件名 | 参数 | 说明 |
|---|---|---|
onSuccess | response: any, file: File | 单个文件上传成功时触发 |
onClick | fileItem: FileItem, index: number | 点击文件项时触发 |
| 名称 | 说明 |
|---|---|
default | 自定义上传区域内容 |
<script setup lang="ts">
const fileList = ref([])
</script>
<template>
<FaFileUpload
v-model="fileList"
action="/api/upload"
/>
</template><script setup lang="ts">
const fileList = ref([])
</script>
<template>
<FaFileUpload
v-model="fileList"
action="/api/upload"
:ext="['jpg', 'png', 'gif']"
/>
</template><script setup lang="ts">
const fileList = ref([])
const maxSize = 5 * 1024 * 1024 // 5MB
</script>
<template>
<FaFileUpload
v-model="fileList"
action="/api/upload"
:size="maxSize"
/>
</template><script setup lang="ts">
const fileList = ref([])
</script>
<template>
<FaFileUpload
v-model="fileList"
action="/api/upload"
:max="3"
multiple
/>
</template><script setup lang="ts">
const fileList = ref([])
</script>
<template>
<FaFileUpload
v-model="fileList"
action="/api/upload"
multiple
/>
</template><script setup lang="ts">
const fileList = ref([])
const token = useToken()
</script>
<template>
<FaFileUpload
v-model="fileList"
action="/api/upload"
method="post"
:headers="{ Authorization: token.value }"
:data="{ userId: 123, type: 'avatar' }"
/>
</template><script setup lang="ts">
const fileList = ref([])
function afterUpload(response: any) {
// 假设后端返回 { code: 200, data: { url: '...' } }
if (response.code === 200) {
return response.data.url
}
throw new Error('上传失败')
}
</script>
<template>
<FaFileUpload
v-model="fileList"
action="/api/upload"
:after-upload="afterUpload"
/>
</template><script setup lang="ts">
const fileList = ref([])
function handleSuccess(response: any, file: File) {
console.log('上传成功:', response)
console.log('文件:', file)
}
</script>
<template>
<FaFileUpload
v-model="fileList"
action="/api/upload"
@on-success="handleSuccess"
/>
</template><script setup lang="ts">
const fileList = ref([])
</script>
<template>
<FaFileUpload
v-model="fileList"
action="/api/upload"
>
<div class="flex flex-col items-center justify-center h-48">
<FaIcon name="i-lucide:cloud-upload" class="text-4xl text-primary mb-4" />
<p class="text-sm text-muted-foreground">点击或拖拽文件到此处上传</p>
</div>
</FaFileUpload>
</template><script setup lang="ts">
const fileList = ref([])
function handleClick(fileItem: FileItem) {
if (fileItem.url) {
// 预览头像
window.open(fileItem.url)
}
}
</script>
<template>
<FaFileUpload
v-model="fileList"
action="/api/upload"
:ext="['jpg', 'png']"
:size="2 * 1024 * 1024"
:max="1"
@on-click="handleClick"
/>
</template><script setup lang="ts">
const fileList = ref<FileItem[]>([])
function handleFileClick(fileItem: FileItem) {
if (fileItem.url) {
window.open(fileItem.url, '_blank')
}
}
</script>
<template>
<FaFileUpload
v-model="fileList"
action="/api/upload"
multiple
:max="10"
@on-click="handleFileClick"
/>
</template>modelValue 是必需的双向绑定属性