Skip to content

Uploader

基础用法

html

<pa-uploader v-model:file-list="fileList" :after-read="onAfterRead" />
ts

import { ref } from 'vue'
import type { FileItem } from '../..'

const fileList = ref<FileItem[]>([])

const onAfterRead = (files: FileItem | FileItem[]) => {
  const file = files as FileItem
  // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  uni.uploadFile({
    url: 'https://example.xxx.com/upload', // 仅为示例,非真实的接口地址
    filePath: file.url,
    name: 'file',
    formData: { user: 'test' },
    success(res) {
      // 上传完成需要更新 fileList
      fileList.value.push({ url: res.data })
    },
  })
}

文件预览

html

<pa-uploader v-model:file-list="fileList" />
ts

import { ref } from 'vue'
// 仅做本地示例,使用时请用网络图片
import img1 from '../../images/demo2.jpeg'
import img2 from '../../images/demo3.jpg'

let src1 = ''
let src2 = ''
// #ifdef MP
const fileManager = uni.getFileSystemManager()
src1 = 'data:image/jpeg;base64,' + fileManager.readFileSync(img1, 'base64')
src2 = 'data:image/jpeg;base64,' + fileManager.readFileSync(img2, 'base64')
// #endif
// #ifdef H5
src1 = img1
src2 = img2
// #endif

const fileList = ref([
  {
    url: src1,
  },
  {
    url: src2,
  },
])

限制上传数量

html

<pa-uploader v-model:file-list="fileList" :max-count="3" />
ts

import { ref } from 'vue'
// 仅做本地示例,使用时请用网络图片
import img1 from '../../images/demo2.jpeg'

let src1 = ''
// #ifdef MP
const fileManager = uni.getFileSystemManager()
src1 = 'data:image/jpeg;base64,' + fileManager.readFileSync(img1, 'base64')
// #endif
// #ifdef H5
src1 = img1
// #endif

const fileList = ref([
  {
    url: src1,
  },
])

限制上传大小

html

<pa-uploader v-model:file-list="fileList" :max-size="1024 * 20" @oversize="onOversize" />
ts

import { ref } from 'vue'
// 仅做本地示例,使用时请用网络图片
import img1 from '../../images/demo2.jpeg'

let src1 = ''
// #ifdef MP
const fileManager = uni.getFileSystemManager()
src1 = 'data:image/jpeg;base64,' + fileManager.readFileSync(img1, 'base64')
// #endif
// #ifdef H5
src1 = img1
// #endif

const fileList = ref([
  {
    url: src1,
  },
])

const onOversize = () => {
  uni.showToast({
    title: '文件大小超过限制(20KB)',
    icon: 'none',
  })
}

自定义上传样式

html

<pa-uploader ref="uploaderRef" v-model:file-list="fileList">
  <pa-button type="primary" icon="plus" @click="onCustomUpload">自定义上传</pa-button>
  <pa-button type="primary" icon="plus" class="ml-20" @click="onUpload">复用原上传</pa-button>
</pa-uploader>
ts

import { ref } from 'vue'
import type { UploaderInstance, FileItem } from '../..'

const uploaderRef = ref<UploaderInstance>()
const fileList = ref<FileItem[]>([])

const onCustomUpload = () => {
  uni.chooseImage({
    count: 10,
    success: ({ tempFilePaths }) => {
      const paths = Array.isArray(tempFilePaths) ? tempFilePaths : [tempFilePaths]
      paths.forEach((path) => {
        fileList.value.push({ url: path })
      })
    },
  })
}

const onUpload = () => {
  // 可以调用组件原有的选择文件功能,适用于想在原有基础上补充分支逻辑的情况
  uploaderRef.value?.chooseFile()
}

禁用文件上传

html

<pa-uploader disabled />

独立控制删除

html

<pa-uploader v-model:file-list="fileList" />
ts

import { ref } from 'vue'
// 仅做本地示例,使用时请用网络图片
import img1 from '../../images/demo2.jpeg'
import img2 from '../../images/demo3.jpg'

let src1 = ''
let src2 = ''
// #ifdef MP
const fileManager = uni.getFileSystemManager()
src1 = 'data:image/jpeg;base64,' + fileManager.readFileSync(img1, 'base64')
src2 = 'data:image/jpeg;base64,' + fileManager.readFileSync(img2, 'base64')
// #endif
// #ifdef H5
src1 = img1
src2 = img2
// #endif

const fileList = ref([
  {
    url: src1,
    deletable: false,
  },
  {
    url: src2,
  },
])

其他文件类型

html

<pa-uploader v-model:file-list="fileList" @click-preview="onPreview" />
ts

import { ref } from 'vue'
import type { FileItem } from '../..'

const fileList = ref<FileItem[]>([
  {
    url: '',
    type: 'word',
  },
  {
    url: '',
    type: 'excel',
  },
  {
    url: '',
    type: 'ppt',
  },
  {
    url: '',
    type: 'pdf',
  },
  {
    url: '',
    type: 'markdown',
  },
  {
    url: '',
    type: 'zip',
  },
  {
    url: '',
    type: 'file',
  },
])

const onPreview = (file: FileItem, index: number) => {
  uni.showToast({
    title: `预览第${index + 1}个文件,是${file.type}文件`,
    icon: 'none',
  })
}

自定义预览

html

<pa-uploader v-model:file-list="fileList" preview-size="100px">
  <template #file="{ file, index, style }">
    <view class="file-item" :style="style">
      <image :src="file.url" :style="style" />
      <view class="mark">图片{{ index + 1 }}</view>
    </view>
  </template>
</pa-uploader>
ts

import { ref } from 'vue'
// 仅做本地示例,使用时请用网络图片
import img1 from '../../images/demo2.jpeg'
import img2 from '../../images/demo3.jpg'

let src1 = ''
let src2 = ''
// #ifdef MP
const fileManager = uni.getFileSystemManager()
src1 = 'data:image/jpeg;base64,' + fileManager.readFileSync(img1, 'base64')
src2 = 'data:image/jpeg;base64,' + fileManager.readFileSync(img2, 'base64')
// #endif
// #ifdef H5
src1 = img1
src2 = img2
// #endif

const fileList = ref([
  {
    url: src1,
  },
  {
    url: src2,
  },
])

Uploader Props

参数说明类型默认值
name标识符,可以在回调函数的第二项参数中获取string | number-
fileList上传的文件列表FileItem[]() => []
mediaType接受的文件类型Array<'image'|'video'|'mix'>['image', 'video', 'mix']
sourceType图片和视频选择的来源Array<'album'|'camera'>['album', 'camera']
sizeType是否压缩所选文件Array<'original'|'compressed'>['original', 'compressed']
maxDuration拍摄视频最长拍摄时间,单位秒。时间范围为 3s 至 60s 之间。不限制相册number10
camera仅在 sourceType 为 camera 时生效,使用前置或后置摄像头'front'|'back''back'
maxSize文件大小限制,单位为bytenumber-
maxCount文件上传数量限制numberNumber.MAX_SAFE_INTEGER
previewSize预览图和上传区域的尺寸,默认单位为pxstring | number-
previewImage是否在上传完成后展示预览图booleantrue
previewFullImage是否在点击预览图后展示全屏图片预览booleantrue
multiple是否开启图片多选boolean-
disabled是否禁用文件上传boolean-
showUpload是否展示文件上传按钮booleantrue
deletable是否展示删除按钮booleantrue
beforeRead文件读取前的回调函数,返回 false 可终止文件读取,支持返回 Promise(file:UniApp.MediaFile[])=>boolean|Promise<unknown>-
afterRead文件读取完成后的回调函数(file:FileItem|FileItem[])=>void-
beforeDelete删除文件之前的钩子,参数为上传的文件和文件列表,若返回 false 或者返回 Promise 且被 reject,则停止删除。(file:FileItem)=>boolean|Promise<unknown>-

Uploader Event

事件名参数
update:fileList(fileList: FileItem[], _name?: string | number)
change(fileList: FileItem[], _name?: string | number)
click-preview(file: FileItem, index: number)
oversize()
delete(file: FileItem, index: number)
error(e: UniApp.GeneralCallbackResult)

Uploader Slot

名称说明
default-

样式变量

名称默认值
--pa-uploader-preview-margin0 10px 10px 0
--pa-uploader-size100px
--pa-uploader-size100px
--pa-uploader-preview-radius4px
--pa-uploader-preview-file-size36px
--pa-uploader-preview-file-colorvar(--pa-color-black-3)
--pa-uploader-preview-file-text-size14px
--pa-uploader-delete-width14px
--pa-uploader-delete-height14px
--pa-uploader-delete-bgrgba(0, 0, 0, 0.7)
--pa-uploader-preview-radius4px
--pa-uploader-delete-icon-size12px
--pa-uploader-size100px
--pa-uploader-size100px
--pa-uploader-preview-margin0 10px 10px 0
--pa-uploader-preview-radius4px