Upload 上传

文件选择上传组件

何时使用

  • 需要上传文件(图片、文档等)时。

导入

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>

更多示例

图片墙

设置 listTypepicture 显示图片缩略图。

<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>

手动触发上传

设置 autoUploadfalse,手动调用上传。

<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>

限制文件大小

通过 maxSizeminSize 限制文件大小(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>

API

Props

参数说明类型默认值
action上传地址string""
fileList文件列表(受控,配合 @change 更新)UploadFile[]-
defaultFileList默认文件列表UploadFile[][]
multiple是否多选booleanfalse
accept接受的文件类型string-
directory文件夹上传booleanfalse
limit限制文件数量number-
maxSize最大文件大小(KB)number-
minSize最小文件大小(KB)number-
listType列表样式list | picturelist
disabled是否禁用booleanfalse
autoUpload自动上传booleantrue
name上传字段名stringfile
data附带数据Record<string, any>{}
headers请求头Record<string, string>{}
withCredentials携带 cookiebooleanfalse
showTooltip文件名提示booleanfalse
showPicInfo显示图片信息booleanfalse
showReplace显示替换图标booleanfalse
prompt提示文本VNode | string-
promptPosition提示位置left | right | bottomright
customRequest自定义请求(options) => void-
beforeUpload上传前校验(file, fileList) => boolean | Promise<boolean>-
previewFile预览文件(file) => Promise<string>-
class附加类名string-

Events

事件说明回调参数
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)

Slots

名称说明参数
default触发器内容-
prompt提示文本-
fileOperation自定义操作区{ file: UploadFile }
fileListTitle文件列表标题-

UploadFile

参数说明类型默认值
uid唯一标识string-
name文件名string-
size文件大小number | string-
status状态uploading | success | error | validating-
url文件 URLstring-
thumbUrl缩略图 URLstring-
percent上传进度number0
response响应数据any-
originFile原始文件File-
preview是否预览boolean-

CustomRequestOptions

参数说明类型
file原始文件File
data附带数据Record<string, any>
headers请求头Record<string, string>
withCredentials携带 cookieboolean
action上传地址string
name字段名string
onProgress进度回调(event: { percent: number }) => void
onSuccess成功回调(response: any) => void
onError失败回调(error: Error) => void