Skip to content

附件上传

业务场景中有需要上传照片或视频的场景

提示

如需从文件管理器中上传其他类型附件,需要自行扩展,uniapp没有提供相关接口。 微信小程序支持从聊天内容中选取其他类型附件

基础用法

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

IMG_1944

<template>
  <sar-space direction="vertical">
    <view class="title">基础使用</view>
    <view class="model-val">双向绑定:{{attachmentValue1}}</view>
    <attachment-upload v-model="attachmentValue1"/>
  </sar-space>
</template>
<script lang="ts" setup>
import AttachmentUpload from "@/components/attachment-upload/index"
const attachmentValue1 = ref<string>()
</script>

按钮形式

设置 mode="button" 即可以按钮形式上传。附件预览以卡片形式展示

IMG_1950

<template>
  <sar-space direction="vertical">
    <view class="title">按钮形式</view>
    <view class="model-val">双向绑定:{{attachmentValue1}}</view>
    <attachment-upload 
     v-model="attachmentValue2"  
     mode="button" 
     button-type="outline" 
     buttonText="上传聊天文件" 
     buttonIsRound 
     buttonIcon="CloudUploadOutlined" 
     buttonIconFamily="outlined"/>
  </sar-space>
</template>
<script lang="ts" setup>
import AttachmentUpload from "@/components/attachment-upload/index"
const attachmentValue2 = ref<string>()
</script>

从聊天框选择仅微信小程序支持

设置 uploadType="file"uploadType="all" 时生效,此模式下只能使用按钮形式。支持通过 extension 属性指定附件类型

IMG_1947

<template>
  <sar-space direction="vertical">
    <view class="title">从聊天框选择</view>
    <view class="model-val">双向绑定:{{attachmentValue1}}</view>
    <attachment-upload v-model="attachmentValue3" uploadType="file" :extension="['pdf']"/>
  </sar-space>
</template>
<script lang="ts" setup>
import AttachmentUpload from "@/components/attachment-upload/index"
const attachmentValue3 = ref<string>()
</script>

数量大小限制

设置 :max-count="2" 指定最多上传数量,设置 :max-size="10 * 1024" 指定单个附件大小

IMG_1948

<template>
  <sar-space direction="vertical">
    <view class="title">数量大小限制</view>
    <view class="model-val">双向绑定:{{attachmentValue1}}</view>
    <attachment-upload v-model="attachmentValue4" :max-count="2" :max-size="10 * 1024"/>
  </sar-space>
</template>
<script lang="ts" setup>
import AttachmentUpload from "@/components/attachment-upload/index"
const attachmentValue4 = ref<string>()
</script>

API

双向绑定

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

属性

属性名称描述类型默认值必填
businessCode业务编码(用于后台附件管理区分附件对应的业务)string-
businessName业务名称(用于后台附件管理区分附件对应的业务)string-
mode模式切换'button' | 'picture''picture'
uploadType可上传附件类型(设置为file会从微信聊天中选取文件,仅微信小程序支持)'image' | 'video' | 'file' | 'all''image'
extension包含的文件后缀,仅 uploadType === 'file' 时生效,数组后缀名不带.string[]-
maxCount最大上传数量number6
maxSize单个附件最大上传大小(单位:字节)number1*1024*1024 (1MB)
disabled禁用状态booleanfalse
readonly只读状态booleanfalse
removable可否删除booleantrue
autoRemove自动删除(点击删除按钮是否自动进行业务删除)booleantrue
removeText删除描述(自动删除开启后,点击删除弹框提示文本)string'删除后无法恢复,是否删除?'
buttonText按钮文本string'点击上传'
buttonType按钮类型'default' | 'pale' | 'mild' | 'outline' | 'text' | 'pale-text''default'
buttonTheme按钮主题'primary' | 'secondary' | 'success' | 'info' | 'warning' | 'danger''primary'
buttonIsRound是否为圆形按钮booleanfalse
buttonIsSquare是否为方形按钮booleanfalse
buttonSize按钮尺寸'mini' | 'small' | 'medium' | 'large''medium'
buttonIcon按钮图标string-
buttonIconFamily按钮图标字体string-

方法

方法名称描述参数
uploadSuccess上传成功触发附件,附件列表
uploadError附件删除时触发附件,错误信息
exceedMaxCount超出附件最大数限制时触发附件列表