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 type filtering, WeChat Mini Program supports filtering when all and file, other platforms do not support)string[]-

Released under the MIT License.

Released under the MIT License.