Skip to content

useUpload

用于处理文件上传和选择相关的逻辑。

基础用法

ts
import { useUpload } from '@/uni_modules/wot-design-uni'

const { startUpload, abort, chooseFile, UPLOAD_STATUS } = useUpload()

// 选择文件
const files = await chooseFile({
  accept: 'image',
  multiple: true,
  maxCount: 9
})

// 开始上传
const file = {
  url: 'file://temp/image.png',
  status: UPLOAD_STATUS.PENDING,
  percent: 0
}

startUpload(file, {
  action: 'https://upload-url',
  onSuccess(res) {
    console.log('上传成功', res)
  },
  onError(err) {
    console.log('上传失败', err) 
  },
  onProgress(progress) {
    console.log('上传进度', progress)
  }
})

// 中断上传
abort()

API

方法

方法名说明参数返回值
startUpload开始上传文件file: UploadFileItem, options: UseUploadOptionsUniApp.UploadTask | void
abort中断上传task?: UniApp.UploadTaskvoid
chooseFile选择文件options: ChooseFileOptionPromise<ChooseFile[]>

UseUploadOptions

参数说明类型默认值
action上传地址string-
header请求头Record<string, any>{}
name文件对应的 keystring'file'
formData其它表单数据Record<string, any>{}
fileType文件类型'image' | 'video' | 'audio''image'
statusCode成功状态码number200
uploadMethod自定义上传方法UploadMethod-
onSuccess上传成功回调Function-
onError上传失败回调Function-
onProgress上传进度回调Function-

ChooseFileOption

参数说明类型默认值
multiple是否支持多选文件booleanfalse
sizeType所选的图片的尺寸Array['original', 'compressed']
sourceType选择文件的来源Array['album', 'camera']
maxCount最大可选数量number9
accept接受的文件类型'image' | 'video' | 'media' | 'file' | 'all''image'
compressed是否压缩视频booleantrue
maxDuration视频最大时长(秒)number60
camera摄像头朝向'back' | 'front''back'

Released under the MIT License.

Released under the MIT License.