Skip to content

Upload 上传

通过点击或者拖拽上传文件。

点击上传

只能上传 jpg/png 文件,且不超过 500kb
vue
<template>
  <x-upload
    v-model:file-list="fileList"
    action="/upload"
  >
    <template #trigger>
      <x-button type="primary">点击上传</x-button>
    </template>
    <template #tip>
      只能上传 jpg/png 文件,且不超过 500kb
    </template>
  </x-upload>
</template>

头像上传

使用 list-type="picture" 显示图片缩略图。

只能上传 jpg/png 文件
vue
<template>
  <x-upload
    v-model:file-list="fileList"
    action="/upload"
    list-type="picture"
  >
    <template #trigger>
      <x-button type="primary">点击上传头像</x-button>
    </template>
  </x-upload>
</template>

照片墙

使用 list-type="picture-card" 显示照片墙样式。

  • +
只能上传 jpg/png 文件
vue
<template>
  <x-upload
    v-model:file-list="fileList"
    action="/upload"
    list-type="picture-card"
  />
</template>

文件缩略图

支持多文件上传
vue
<template>
  <x-upload
    v-model:file-list="fileList"
    action="/upload"
    list-type="picture"
    multiple
  />
</template>

图片列表缩略图

  • +
最多上传3个文件
vue
<template>
  <x-upload
    v-model:file-list="fileList"
    action="/upload"
    list-type="picture-card"
    :limit="3"
    :on-exceed="handleExceed"
  />
</template>

上传文件列表控制

通过 limiton-exceed 限制上传文件数量。

vue
<template>
  <x-upload
    v-model:file-list="fileList"
    action="/upload"
    :limit="3"
    :on-exceed="handleExceed"
    multiple
  />
</template>

<script setup>
const handleExceed = (files) => {
  alert(`最多只能上传3个文件`)
}
</script>

拖拽上传

设置 drag 属性启用拖拽上传。

📁
将文件拖到此处,或点击上传
将文件拖到此处,或点击上传
vue
<template>
  <x-upload
    v-model:file-list="fileList"
    action="/upload"
    drag
    multiple
  />
</template>

手动上传

设置 auto-upload="false" 禁用自动上传,通过 submit 方法手动触发上传。

只有点击"开始上传"后才会真正上传
vue
<template>
  <x-upload
    ref="uploadRef"
    v-model:file-list="fileList"
    action="/upload"
    :auto-upload="false"
  >
    <template #trigger>
      <x-button type="primary">选取文件</x-button>
    </template>
    <x-button @click="uploadRef?.submit()">开始上传</x-button>
  </x-upload>
</template>

<script setup>
import { ref } from 'vue'

const uploadRef = ref()
</script>

API

Props

属性说明类型默认值
action上传的地址string'#'
headers设置上传的请求头部Record<string, string>
data上传时附带的额外参数Record<string, any>
multiple是否支持多选文件booleanfalse
accept接受上传的文件类型string
limit最大允许上传个数number
disabled是否禁用booleanfalse
drag是否启用拖拽上传booleanfalse
listType文件列表的类型'text' | 'picture' | 'picture-card''text'
autoUpload是否在选取文件后立即进行上传booleantrue
fileList上传的文件列表UploadFile[][]
showFileList是否显示文件列表booleantrue
withCredentials支持发送 cookie 凭证信息booleanfalse
name上传的文件字段名string'file'
httpRequest覆盖默认的上传行为(options) => Promise
beforeUpload上传文件之前的钩子(file, fileList) => boolean | Promise
beforeRemove删除文件之前的钩子(file, fileList) => boolean

Events

事件名说明回调参数
success文件上传成功时的钩子(response, file, fileList)
error文件上传失败时的钩子(error, file, fileList)
progress文件上传时的钩子(event, file, fileList)
change文件状态改变时的钩子(file, fileList)
remove文件列表移除文件时的钩子(file, fileList)
exceed文件超出个数限制时的钩子(files, fileList)

Methods

方法名说明参数
submit手动上传文件列表
clearFiles清空已上传的文件列表

Slots

插槽名说明
trigger触发文件选择框的内容
tip提示说明文字
drag拖拽区域内容

UploadFile 数据结构

字段说明类型
name文件名string
size文件大小number
url文件地址string
status文件状态'ready' | 'uploading' | 'success' | 'error'
percentage上传进度number
uid文件唯一标识number | string
raw原始文件对象File
response服务器响应any
thumbUrl缩略图地址string

基于 MIT 许可发布