Skip to content

Upload 上传

基于 ElUpload 封装,集成文件类型校验、大小限制、进度模拟和预览功能。

基础用法

vue
<template>
  <div>
    <MUpload
      v-model="fileList"
      list-type="picture-card"
      accept=".png,.jpg,.jpeg"
      :max="5"
      :max-size="2"
    ></MUpload>
    <pre>{{ fileList }}</pre>

    <MMarkdownView :value="readme"></MMarkdownView>
  </div>
</template>

<script setup lang="ts">
import readme from '@/components/upload/README.md?raw'
import type { UploadFile } from '@/components'
import { ref } from 'vue'

const fileList = ref<UploadFile[]>([
  {
    name: 'food.jpeg',
    url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
  }
])
</script>

<style lang="scss" scoped></style>

API

Upload 属性

属性名说明类型默认值
modelValuev-model 文件列表UploadFile[][]
multiple批量上传booleantrue
accept接收上传的文件类型string'all'
max文件数量限制numberInfinity
maxSize文件大小限制(MB)numberInfinity
listType文件展示形态'text' | 'picture' | 'picture-card''picture'
disabled是否禁用booleanfalse
rules文件上传前的校验UploadRule[][]
preview预览功能(file) => void | booleantrue
http文件上传接口FunctionuploadCommon
httpFileKeyFormData 的 keystring'file'
showMessage显示上传结果提示booleantrue

UploadFile

属性名说明类型默认值
name文件名string
url文件地址string
response原始响应数据object
percentage上传进度number
status文件状态'ready' | 'uploading' | 'success' | 'fail''success'

UploadRule

属性名说明类型默认值
validator校验函数,返回 true 则校验失败(file: File) => boolean
message校验失败提示string

Upload 事件

事件名说明类型
change文件上传、移除时触发(fileList: UploadFile[]) => void

Upload 插槽

插槽名说明
default开启文件选择对话框的按钮
tip提示文字

基于 MIT 许可发布