Skip to content

useUpload

Used for handling file upload and selection related logic.

Basic Usage

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

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

// Select 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-
chooseFileSelect fileoptions: ChooseFileOptionPromise<ChooseFile[]>-

UseUploadOptions

ParameterDescriptionTypeDefault ValueMinimum Version
actionUpload addressstring--
headerRequest headersRecord<string, any>{}-
nameFile corresponding keystring'file'-
formDataOther form dataRecord<string, any>{}-
fileTypeFile type'image' | 'video' | 'audio''image'-
statusCodeSuccess status codenumber | number[]200-
uploadMethodCustom upload methodUploadMethod--
onSuccessUpload success callbackFunction--
onErrorUpload failure callbackFunction--
onProgressUpload progress callbackFunction--

ChooseFileOption

ParameterDescriptionTypeDefault ValueMinimum Version
multipleWhether to support multiple file selectionbooleanfalse-
sizeTypeSelected image sizeArray['original', 'compressed']-
sourceTypeFile selection sourceArray['album', 'camera']-
maxCountMaximum selectable quantitynumber9-
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 type filtering, WeChat mini program supports filtering when all and file, other platforms not supported)string[]-

File Selection Quantity Limits

Different platforms have different limits on file selection quantity, which are determined by the uni-app platform API itself:

WeChat Platform

Selection MethodMaximum QuantityDescriptionApplicable Scenario
chooseMedia20Maximum quantity limit when selecting images and videosUsed when accept is image, video, media
chooseMessageFile100Maximum quantity limit when selecting filesUsed when accept is file, all

H5 Platform

Selection MethodMaximum QuantityDescriptionApplicable Scenario
chooseImage9Maximum quantity limit when selecting imagesUsed when accept is image
chooseVideo1Does not support multiple selection, can only select single video fileUsed when accept is video
chooseFile100Maximum quantity limit when selecting filesUsed when accept is all

H5 Platform Special Note

The count value on H5 platform is based on browser specifications. Current test results show that it can only limit single/multiple selection, but cannot limit specific quantity. Moreover, in actual mobile browsers, few support multiple selection.

Other Platforms

Selection MethodMaximum QuantityDescriptionApplicable Scenario
chooseImage9Maximum quantity limit when selecting imagesUsed when accept is image
chooseVideo1Does not support multiple selection, can only select single video fileUsed when accept is video

Hint

  • WeChat platform prioritizes using chooseMedia and chooseMessageFile, with higher selection quantity limits
  • Video selection does not support multiple selection on most platforms
  • Actual selectable quantity is also further limited by the maxCount parameter

Released under the MIT License.

Released under the MIT License.