Skip to content

useUpload

Used for handling file upload and selection related logic.

Basic Usage

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

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

// Choose files
const files = await chooseFile({
  accept: 'image',
  multiple: true,
  maxCount: 9
})

// Start upload
const file = {
  url: 'file://temp/image.png',
  status: UPLOAD_STATUS.PENDING,
  percent: 0
}

startUpload(file, {
  action: 'https://upload-url',
  onSuccess(res) {
    console.log('Upload successful', res)
  },
  onError(err) {
    console.log('Upload failed', err) 
  },
  onProgress(progress) {
    console.log('Upload progress', progress)
  }
})

// Abort upload
abort()

API

Methods

Method NameDescriptionParametersReturn ValueMinimum Version
startUploadStart uploading filefile: UploadFileItem, options: UseUploadOptionsUniApp.UploadTask | void-
abortAbort uploadtask?: UniApp.UploadTaskvoid-
chooseFileChoose fileoptions: ChooseFileOptionPromise<ChooseFile[]>-

UseUploadOptions

ParameterDescriptionTypeDefaultMinimum Version
actionUpload URLstring--
headerRequest headersRecord<string, any>{}-
nameKey corresponding to the filestring'file'-
formDataOther form dataRecord<string, any>{}-
fileTypeFile type'image' | 'video' | 'audio''image'-
statusCodeSuccess status codenumber200-
uploadMethodCustom upload methodUploadMethod--
onSuccessUpload success callbackFunction--
onErrorUpload failure callbackFunction--
onProgressUpload progress callbackFunction--

ChooseFileOption

ParameterDescriptionTypeDefaultMinimum Version
multipleWhether to support multiple file selectionbooleanfalse-
sizeTypeSize of selected imagesArray['original', 'compressed']-
sourceTypeSource of file selectionArray['album', 'camera']-
maxCountMaximum number of selectionsnumber9-
acceptAccepted file types'image' | 'video' | 'media' | 'file' | 'all''image'-
compressedWhether to compress videobooleantrue-
maxDurationMaximum video duration (seconds)number60-
cameraCamera direction'back' | 'front''back'-
extensionFilter by file extension (H5 supports all types; WeChat Mini Program supports filtering when accept is 'all' or 'file'; other platforms do not support)string[]--

File Selection Quantity Limits

Different platforms have different file selection methods with varying maximum quantity limits, which are determined by the uni-app platform APIs:

WeChat Platform

WeChat Mini Program platform offers richer file selection capabilities with higher quantity limits:

Selection MethodMaximum CountDescriptionApplicable File Types
chooseMedia20Maximum selection count for images and videosUsed when accept is image, video, or media
chooseMessageFile100Maximum selection count for files from client sessionsUsed when accept is file or all

H5 Platform

H5 platform supports multiple file selection methods:

Selection MethodMaximum CountDescriptionApplicable File Types
chooseImage9Maximum selection count for imagesUsed when accept is image
chooseVideo1Does not support multiple selection, single video file onlyUsed when accept is video
chooseFile100Maximum selection count for filesUsed when accept is all

Other Platforms

Other platforms (such as Alipay Mini Program, DingTalk Mini Program, App, etc.) have relatively limited file selection capabilities:

Selection MethodMaximum CountDescriptionApplicable File Types
chooseImage9Maximum selection count for imagesUsed when accept is image
chooseVideo1Does not support multiple selection, single video file onlyUsed when accept is video

Tips

  • When the set maxCount exceeds the above platform limits, the actual selection count will be subject to platform limits
  • The chooseFile function will automatically choose the optimal method based on platform capabilities
  • WeChat Mini Program platform prioritizes using chooseMedia for selecting images and videos, which has higher selection count limits
  • Video selection on non-WeChat platforms is limited by the chooseVideo API and only supports single selection
  • Platform capability priority: WeChat Platform > H5 Platform > Other Platforms

maxCount Parameter Limitation

The maxCount parameter in ChooseFileOption is limited by the underlying platform APIs. Setting a value higher than the platform limit will be automatically capped to the maximum supported value.

Released under the MIT License.

Released under the MIT License.