Skip to content

H5预览

APP下载

Upload 文件上传

一个功能完整的上传组件,已封装好上传请求处理

  • 图片上传:支持配置 上传插件 实现多种云存储方式:七牛云、阿里云 OSS、腾讯云 COS、MinIO、亚马逊 AWS 等。

  • 文档上传(开发中)

基础参数

参数说明类型可选值默认值
pt样式穿透配置PassThrough--
modelValue双向绑定的文件地址string | string[]--
icon上传按钮显示的图标string-"camera-fill"
text上传按钮显示的文字string-"上传/拍摄"
sizeType图片压缩方式string[]"original" | "compressed"["original", "compressed"]
sourceType图片选择来源string[]"album" | "camera"["album", "camera"]
height上传区域高度string | number-150
width上传区域宽度string | number-150
multiple是否支持多文件上传boolean-false
limit最大上传文件数量number-9
disabled是否禁用上传功能boolean-false

事件

事件名称说明回调参数
change文件列表变化时触发value: string | string[]
exceed超出文件数量限制时触发list: ClUploadItem[]
success文件上传成功时触发url: string
error文件上传失败时触发message: string
progress上传进度变化时触发progress: number
ts
type ClUploadItem = {
  uid: string;
  preview: string;
  url: string;
  progress: number;
};

PassThrough

样式穿透配置允许您自定义组件内部各个元素的样式,实现更灵活的外观定制。

参数说明类型
className组件根元素样式string
item文件项容器配置PassThroughProps
add添加按钮配置PassThroughProps
image图片预览配置PassThroughProps
text按钮文本配置PassThroughProps

使用示例

基础用法

最简单的使用方式,绑定一个字符串变量来接收上传后的文件地址。

html
<cl-upload v-model="url"></cl-upload>

禁用状态

通过 disabled 属性禁用上传功能,常用于表单只读状态。

html
<cl-upload v-model="url" disabled></cl-upload>

自定义图标和文字

通过 icontext 属性自定义上传按钮的外观。

html
<cl-upload v-model="url" icon="id-card-line" text="上传证件照"></cl-upload>

自定义尺寸

通过 heightwidth 属性调整上传区域的大小。

html
<cl-upload v-model="url" :width="300" :height="200"></cl-upload>

多文件上传

开启 multiple 后支持选择多个文件,此时绑定值必须是字符串数组。

vue
<template>
  <cl-upload v-model="urls" multiple></cl-upload>
</template>

<script lang="ts" setup>
import { ref } from "vue";

const urls = ref<string[]>([]);
</script>

限制上传数量

通过 limit 属性限制最大上传文件数量,超出限制时会触发 exceed 事件。

vue
<template>
  <cl-upload
    v-model="urls"
    multiple
    :limit="3"
    @exceed="handleExceed"
  ></cl-upload>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import type { ClUploadItem } from "@/uni_modules/cool-ui";

const urls = ref<string[]>([]);

function handleExceed(list: ClUploadItem[]) {
  console.log("超出文件数量限制", list);
}
</script>

监听上传事件

监听上传过程中的各种事件,提供更好的用户体验。

vue
<template>
  <cl-upload
    v-model="url"
    @progress="handleProgress"
    @success="handleSuccess"
    @error="handleError"
  ></cl-upload>
</template>

<script lang="ts" setup>
import { ref } from "vue";

const url = ref<string>("");

function handleProgress(progress: number) {
  console.log("上传进度:", progress);
}

function handleSuccess(url: string) {
  console.log("上传成功:", url);
}

function handleError(message: string) {
  console.error("上传失败:", message);
}
</script>