import { Upload } from "@reglow/reui";
import type { UploadFile } from "@reglow/reui";
点击上传按钮,选择文件后自动上传。
<script setup>
import { Upload } from "@reglow/reui";
</script>
<template>
<Upload action="https://api.example.com/upload">
<button class="px-4 py-2 rounded-md border bg-background text-sm">
点击上传
</button>
</Upload>
</template>
设置 listType 为 picture 显示图片缩略图。
<script setup>
const defaultFileList = [
{
uid: "1",
name: "image.png",
status: "success",
url: "/image.png",
thumbUrl: "/image.png",
},
];
</script>
<template>
<Upload
action="/api/upload"
list-type="picture"
:default-file-list="defaultFileList"
:limit="6"
/>
</template>
传入 fileList 作为受控组件,配合 @change 使用。
<script setup>
import { ref } from "vue";
const fileList = ref([
{ uid: "1", name: "file.jpeg", status: "success", size: 130000, thumbUrl: "/file.jpeg" },
]);
const onChange = ({ fileList: newList }) => {
fileList.value = newList;
};
</script>
<template>
<Upload
v-model:file-list="fileList"
action="/api/upload"
list-type="picture"
@change="onChange"
/>
</template>
<template>
<Upload action="/api/upload" multiple>
<div class="w-80 h-32 flex flex-col items-center justify-center border-2 border-dashed rounded-lg text-muted-foreground hover:bg-accent/30 cursor-pointer">
<UploadCloud class="size-8 mb-2" />
<span class="text-sm">点击或拖拽文件到此处上传</span>
</div>
</Upload>
</template>
通过 customRequest 完全自定义上传逻辑。
<script setup>
const customRequest = ({ file, onSuccess, onProgress, onError }) => {
const formData = new FormData();
formData.append("file", file);
const xhr = new XMLHttpRequest();
xhr.upload.onprogress = (e) => {
if (e.lengthComputable) {
onProgress({ percent: Math.round((e.loaded / e.total) * 100) });
}
};
xhr.onload = () => {
if (xhr.status === 200) {
onSuccess(JSON.parse(xhr.responseText));
} else {
onError(new Error("上传失败"));
}
};
xhr.open("POST", "/api/custom-upload");
xhr.send(formData);
};
</script>
<template>
<Upload action="" :custom-request="customRequest">
<button class="px-4 py-2 rounded-md border text-sm">自定义上传</button>
</Upload>
</template>
通过 beforeUpload 在上传前进行自定义校验。
<script setup>
const beforeUpload = (file) => {
const isImage = file.type.startsWith("image/");
if (!isImage) {
alert("只能上传图片文件");
return false;
}
return true;
};
</script>
<template>
<Upload action="/api/upload" :before-upload="beforeUpload">
<button class="px-4 py-2 rounded-md border text-sm">上传图片</button>
</Upload>
</template>
设置 autoUpload 为 false,手动调用上传。
<script setup>
import { ref } from "vue";
const uploadRef = ref();
const fileList = ref([]);
</script>
<template>
<Upload
ref="uploadRef"
action="/api/upload"
:auto-upload="false"
v-model:file-list="fileList"
>
<button class="px-4 py-2 rounded-md border text-sm">选择文件</button>
</Upload>
<button @click="() => fileList.forEach(f => uploadRef?.uploadFile?.(f))">
开始上传
</button>
</template>
通过 maxSize 和 minSize 限制文件大小(KB)。
<template>
<Upload
action="/api/upload"
:max-size="5120"
:min-size="10"
@size-error="(file) => alert(`${file.name} 大小不符合要求`)"
>
<button class="px-4 py-2 rounded-md border text-sm">上传(10KB ~ 5MB)</button>
</Upload>
</template>
| 参数 | 说明 | 类型 | 默认值 |
|---|
| action | 上传地址 | string | "" |
| fileList | 文件列表(受控,配合 @change 更新) | UploadFile[] | - |
| defaultFileList | 默认文件列表 | UploadFile[] | [] |
| multiple | 是否多选 | boolean | false |
| accept | 接受的文件类型 | string | - |
| directory | 文件夹上传 | boolean | false |
| limit | 限制文件数量 | number | - |
| maxSize | 最大文件大小(KB) | number | - |
| minSize | 最小文件大小(KB) | number | - |
| listType | 列表样式 | list | picture | list |
| disabled | 是否禁用 | boolean | false |
| autoUpload | 自动上传 | boolean | true |
| name | 上传字段名 | string | file |
| data | 附带数据 | Record<string, any> | {} |
| headers | 请求头 | Record<string, string> | {} |
| withCredentials | 携带 cookie | boolean | false |
| showTooltip | 文件名提示 | boolean | false |
| showPicInfo | 显示图片信息 | boolean | false |
| showReplace | 显示替换图标 | boolean | false |
| prompt | 提示文本 | VNode | string | - |
| promptPosition | 提示位置 | left | right | bottom | right |
| customRequest | 自定义请求 | (options) => void | - |
| beforeUpload | 上传前校验 | (file, fileList) => boolean | Promise<boolean> | - |
| previewFile | 预览文件 | (file) => Promise<string> | - |
| class | 附加类名 | string | - |
| 事件 | 说明 | 回调参数 |
|---|
| change | 文件变化 | ({ fileList, file }) |
| success | 上传成功 | (response, file) |
| error | 上传失败 | (error, file) |
| progress | 上传进度 | (percent, file) |
| exceed | 超出限制 | (files: File[]) |
| sizeError | 大小错误 | (file: File) |
| acceptInvalid | 类型不符 | (file: File) |
| remove | 移除文件 | (file: UploadFile) |
| preview | 预览文件 | (file: UploadFile) |
| 名称 | 说明 | 参数 |
|---|
| default | 触发器内容 | - |
| prompt | 提示文本 | - |
| fileOperation | 自定义操作区 | { file: UploadFile } |
| fileListTitle | 文件列表标题 | - |
| 参数 | 说明 | 类型 | 默认值 |
|---|
| uid | 唯一标识 | string | - |
| name | 文件名 | string | - |
| size | 文件大小 | number | string | - |
| status | 状态 | uploading | success | error | validating | - |
| url | 文件 URL | string | - |
| thumbUrl | 缩略图 URL | string | - |
| percent | 上传进度 | number | 0 |
| response | 响应数据 | any | - |
| originFile | 原始文件 | File | - |
| preview | 是否预览 | boolean | - |
| 参数 | 说明 | 类型 |
|---|
| file | 原始文件 | File |
| data | 附带数据 | Record<string, any> |
| headers | 请求头 | Record<string, string> |
| withCredentials | 携带 cookie | boolean |
| action | 上传地址 | string |
| name | 字段名 | string |
| onProgress | 进度回调 | (event: { percent: number }) => void |
| onSuccess | 成功回调 | (response: any) => void |
| onError | 失败回调 | (error: Error) => void |