Skip to content

附件上传 ​

表单中需要附件上传时使用

基础用法 ​

引入组件 import AttachmentUpload from "@/components/attachment-upload/index.vue" 使用v-model进行双向绑定即可。双向绑定的数据为逗号分隔格式,返回的数据为后台附件表sys_attachment的主键id

image-20250221151925930

vue
<template>
  <a-flex vertical :gap="8">
    <a-typography-title :level="4">基础用法</a-typography-title>
    <a-typography-text>绑定数据:{{modelValue}}</a-typography-text>
    <attachment-upload v-model="modelValue"/>
  </a-flex>
</template>

<script setup lang="ts">
import AttachmentUpload from "@/components/attachment-upload/index.vue";
import {ref} from "vue";
const modelValue = ref<string>('')
</script>

图片预览 ​

设置mode="picture"即可

image-20250221152410921

vue
<template>
  <a-flex vertical :gap="8">
    <a-typography-title :level="4">图片预览</a-typography-title>
    <a-typography-text>绑定数据:{{modelValue}}</a-typography-text>
    <attachment-upload v-model="modelValue" mode="picture"/>
  </a-flex>
</template>

<script setup lang="ts">
import AttachmentUpload from "@/components/attachment-upload/index.vue";
import {ref} from "vue";
const modelValue = ref<string>('')
</script>

拖拽上传 ​

设置mode="dragger"即可

image-20250221152553597

vue
<template>
  <a-flex vertical :gap="8">
    <a-typography-title :level="4">拖拽上传</a-typography-title>
    <a-typography-text>绑定数据:{{modelValue}}</a-typography-text>
    <attachment-upload v-model="modelValue" mode="dragger"/>
  </a-flex>
</template>

<script setup lang="ts">
import AttachmentUpload from "@/components/attachment-upload/index.vue";
import {ref} from "vue";
const modelValue = ref<string>('')
</script>

分片上传 ​

分片上传也支持图片预览、拖拽上传模式

设置chunk 即可,还可以设置每个片段大小,分片同时上传数量。分片上传会对文件计算 md5(hash-wasm),上传期间组件内展示「处理中/上传中/合并中」进度条;分片模式下不支持多选与文件夹上传

image-20250221152744912

vue
<template>
  <a-flex vertical :gap="8">
    <a-typography-title :level="4">分片上传</a-typography-title>
    <a-typography-text>绑定数据:{{modelValue}}</a-typography-text>
    <attachment-upload v-model="modelValue" :max-size="100" chunk :chunk-size="10" :chunk-upload-count="2"/>
  </a-flex>
</template>

<script setup lang="ts">
import AttachmentUpload from "@/components/attachment-upload/index.vue";
import {ref} from "vue";
const modelValue = ref<string>('')
</script>

业务删除 ​

设置 auto-remove 后,点击附件列表的删除按钮会弹确认框,确认后自动执行业务删除并回传业务结果;也可通过 ref 调用组件暴露的 businessRemove 方法手动触发。未开启时删除仅从列表移除,回传列表内移除的文件对象

API ​

双向绑定 ​

属性名称描述类型默认值是否必填
v-model双向绑定string''是

属性 ​

属性名称描述类型默认值是否必填
mode模式:按钮/图片/拖拽'button' | 'picture' | 'dragger''button'否
icon附件图标string(图标组件名称)-否
text描述文本string-否
uploadType可上传的附件类型string[]-否
description详细说明(仅支持拖拽上传)string-否
maxCount最大上传数量(超出后不会调用上传)number10否
maxSize最大上传大小,分片上传注意调整大小(mb)number10否
multiple是否支持多附件上传(分片上传不支持)booleantrue否
directory是否支持附件夹上传(分片上传不支持)booleanfalse否
businessCode业务编码string路由名称否
businessName业务名称string菜单名称否
chunk是否分片上传booleanfalse否
chunkSize最大分片片段大小number20否
chunkUploadCount分片上传同时上传数量number3否
fileName指定附件名称(自动保留原附件扩展名)string-否
autoRemove自动删除(点击删除按钮是否自动进行业务删除)booleanfalse否
public公开附件模式:上传公开附件,双向绑定收发附件 path(对象键)而非附件 id;免登录可下载(App 安装包等),列表删除仅解除引用不做业务删除booleanfalse否

事件 ​

事件名称描述回调参数
uploadSuccess上传成功触发
remove附件移除时触发列表内移除回调整文件对象;autoRemove 确认删除回传 { id, status } 业务结果
uploadError上传失败触发(file, errorMsg?)
exceedMaxCount超出附件最大数限制被拒收时触发超出的附件

方法 ​

方法名称描述参数
businessRemove手动触发业务删除-