Skip to content

Upload

Image, video and file upload component.

Tip

Currently, all platforms compatible with the component library support video upload. Video covers implemented using the video component are supported on H5, WeChat Mini Program and Alipay Mini Program platforms, while DingTalk Mini Program and App platforms are limited by the video tag capability and cannot directly serve as video covers. It is recommended to get the video cover in the change event and supplement the thumb field for the corresponding video in fileList.

About WeChat Mini Program Privacy Agreement

upload uses three privacy interfaces on the WeChat Mini Program platform: wx.chooseImage, wx.chooseMedia, wx.chooseVideo. It needs to be configured according to the WeChat privacy agreement, otherwise the upload capability will be unavailable. Please refer to Mini Program Privacy Agreement Development Guide.

Component Types

Basic Usage

Set the upload list through file-list or v-model:file-list, and specify the upload address through action.

html
<wd-upload v-model:file-list="fileList" accept="image" image-mode="aspectFill" :action="action" :success-status="[200, 201]"></wd-upload>
ts
import { ref } from 'vue'
import type { UploadFile } from '@/uni_modules/wot-ui/components/wd-upload/types'

const action = 'https://69bd04402bc2a25b22ad0a49.mockapi.io/upload'

const fileList = ref<UploadFile[]>([
  {
    url: 'https://wot-ui.cn/assets/panda.jpg'
  }
])

Upload Video

After setting accept to video, you can upload video files.

html
<wd-upload accept="video" multiple :file-list="fileList" :action="action" :success-status="[200, 201]" @change="handleChange"></wd-upload>

Upload Media and Files

accept="media" supports images and videos, accept="file" supports regular files, accept="all" supports all file types. Different platforms have different support ranges, see the accept valid values description below.

html
<wd-upload accept="all" multiple :file-list="fileList" :action="action" :success-status="[200, 201]" @change="handleChange"></wd-upload>

Component States

Upload Status Hooks

Upload status feedback can be displayed through success, fail and progress events.

html
<wd-upload
  :file-list="fileList"
  :action="action"
  :success-status="[200, 201]"
  @change="handleChange"
  @success="handleSuccess"
  @fail="handleFail"
  @progress="handleProgress"
></wd-upload>

Disabled

html
<wd-upload :file-list="fileList" disabled :action="action" :success-status="[200, 201]" @change="handleChange"></wd-upload>

Manual Trigger Upload ^(1.3.8)

After setting auto-upload="false", you can manually start upload through the component instance's submit method.

html
<wd-upload
  ref="uploadRef"
  :auto-upload="false"
  :file-list="fileList"
  :action="action"
  :success-status="[200, 201]"
  @change="handleChange"
></wd-upload>

<wd-button @click="uploadRef?.submit()">Start Upload</wd-button>
ts
import { ref } from 'vue'
import type { UploadFile, UploadInstance } from '@/uni_modules/wot-ui/components/wd-upload/types'

const uploadRef = ref<UploadInstance>()
const fileList = ref<UploadFile[]>([])

Component Variants

Maximum Upload Limit

Limit the maximum number of files through limit.

html
<wd-upload :file-list="fileList" :limit="3" :action="action" :success-status="[200, 201]" @change="handleChange"></wd-upload>

Overwrite Upload ^(1.5.0)

After setting reupload, re-selecting files will replace the previous item, suitable for single file overwrite scenarios like avatars.

html
<wd-upload reupload v-model:file-list="fileList" accept="image" image-mode="aspectFill" :action="action" :success-status="[200, 201]"></wd-upload>

Multiple Selection

html
<wd-upload :file-list="fileList" multiple :action="action" :success-status="[200, 201]" @change="handleChange"></wd-upload>

Component Styles

Custom Evoke Style

The default upload evoke area can be replaced through the default slot.

html
<wd-upload :file-list="fileList" :limit="5" :action="action" :success-status="[200, 201]" @change="handleChange">
  <wd-button>Custom Evoke Style</wd-button>
</wd-upload>

Custom Preview Style ^(1.3.12)

Content that covers the preview area can be customized through the preview-cover slot.

html
<wd-upload v-model:file-list="fileList" accept="image" image-mode="aspectFill" :action="action" :success-status="[200, 201]">
  <template #preview-cover="{ file, index }">
    <view class="preview-cover">{{ file.name || `File${index}` }}</view>
  </template>
</wd-upload>
scss
.preview-cover {
  margin-top: 10rpx;
  text-align: center;
}

Special Styles

Intercept Preview Image Operation

before-preview is triggered before preview. Returning false or a Promise with value false can prevent preview.

html
<wd-upload :file-list="fileList" :action="action" :success-status="[200, 201]" @change="handleChange" :before-preview="beforePreview"></wd-upload>

Upload Pre-processing

before-upload is triggered after confirming file selection and before initiating upload.

html
<wd-upload :file-list="fileList" :action="action" :success-status="[200, 201]" @change="handleChange" :before-upload="beforeUpload"></wd-upload>

Remove Image Pre-processing

html
<wd-upload :file-list="fileList" :action="action" :success-status="[200, 201]" @change="handleChange" :before-remove="beforeRemove"></wd-upload>

Select File Pre-processing

html
<wd-upload :file-list="fileList" :action="action" :success-status="[200, 201]" @change="handleChange" :before-choose="beforeChoose"></wd-upload>

Custom Upload Method ^(1.3.8)

Upload behavior can be completely taken over through upload-method.

html
<wd-upload v-model:file-list="fileList" :upload-method="customUpload" :success-status="[200, 201]"></wd-upload>
ts
import type { UploadMethod } from '@/uni_modules/wot-ui/components/wd-upload/types'

const customUpload: UploadMethod = (file, formData, options) => {
  const task = uni.uploadFile({
    url: options.action,
    name: options.name,
    filePath: file.url,
    formData,
    header: options.header,
    success: (response) => options.onSuccess(response, file, formData),
    fail: (error) => options.onError(error, file, formData)
  })

  task.onProgressUpdate((response) => {
    options.onProgress(response, file)
  })

  return task
}

Filter by File Extension ^(1.9.0)

After setting extension, you can limit the suffix of selected files. H5 supports all type filtering, WeChat Mini Program supports all and file scenario filtering.

html
<wd-upload v-model:file-list="fileList" :extension="['.jpg', '.png']" :action="action" :success-status="[200, 201]"></wd-upload>

Business Capabilities

Upload to Cloud Storage ^(0.1.61)

build-form-data is used to dynamically build signature fields before actual upload, suitable for connecting to OSS, COS, OBS and other cloud storage services.

html
<wd-upload :file-list="files" :action="host" :build-form-data="buildFormData" @change="handleChange"></wd-upload>
ts
const host = 'https://examplebucket.oss-cn-zhangjiakou.aliyuncs.com'

function buildFormData({ file, formData }) {
  const imageName = file.url.substring(file.url.lastIndexOf('/') + 1)

  return {
    ...formData,
    key: `20231120/${imageName}`,
    OSSAccessKeyId: 'your-access-key',
    policy: 'your-policy',
    signature: 'your-signature',
    success_action_status: '200'
  }
}

Upload Attributes

ParameterDescriptionTypeDefault Value
file-list / v-model:file-list v1.3.8Upload file list, for example [{ url: 'https://xxx.cdn.com/xxx.jpg' }]UploadFile[][]
actionUpload addressstring''
headerUpload request headerRecord<string, any>{}
multipleWhether to support multiple file selectionbooleanfalse
disabledWhether disabledbooleanfalse
limitMaximum allowed upload countnumber-
show-limit-numWhen limiting upload quantity, whether to show current quantitybooleantrue
max-sizeFile size limit, in bytesnumberNumber.MAX_VALUE
source-typeSelect image sourceUploadSourceType[]['album', 'camera']
size-typeSelected image sizeUploadSizeType[]['original', 'compressed']
nameUpload file field namestringfile
form-dataAdditional form data attached to HTTP requestRecord<string, any>{}
on-preview-failPreview failure callbackUploadOnPreviewFail-
before-uploadUpload pre-hookUploadBeforeUpload-
before-chooseSelect file pre-hookUploadBeforeChoose-
before-removeDelete file pre-hookUploadBeforeRemove-
before-previewImage preview pre-hookUploadBeforePreview-
build-form-data v0.1.61Hook for dynamically building upload formDataUploadBuildFormData-
loading-typeLoading icon typeLoadingTypecircular
loading-colorLoading icon colorstring#ffffff
loading-sizeLoading icon sizestring24px
accept v1.3.0Accepted file types, optional values are image, video, media, all, fileUploadFileTypeimage
status-keyThe key corresponding to the status field in file data structurestringstatus
compressed v1.3.0Whether to compress video, effective when accept is video or mediabooleantrue
max-duration v1.3.0Maximum video recording duration, in secondsnumber60
camera v1.3.0Use front or back camera, optional values are front, backUploadCameraTypeback
image-modemode property for preview imageImageModeaspectFit
success-status v1.3.4Interface response success status codenumber | number[]200
custom-evoke-classCustom upload button style classstring''
custom-preview-classCustom preview list style classstring''
auto-upload v1.3.8Whether to auto-upload after selecting files, needs to manually call submit when closedbooleantrue
reupload v1.5.0Whether to enable overwrite upload, will close image preview when enabledbooleanfalse
upload-method v1.3.8Custom upload methodUploadMethod-
extension v1.9.0Filter by file extension, H5 supports all type filtering, WeChat Mini Program supports all and file scenario filteringstring[]-
custom-classRoot node custom style classstring''
custom-styleRoot node custom stylestring''

accept Valid Values

ParameterDescriptionTypeDefault Value
imageImage, supported on all platforms, WeChat Mini Program uses chooseMedia implementationUploadFileType-
video v1.3.0Video, supported on all platforms, WeChat Mini Program uses chooseMedia implementationUploadFileType-
media v1.3.0Image and video, only supported on WeChat Mini Program, uses chooseMedia implementationUploadFileType-
file v1.3.0Regular file, only supported on WeChat Mini Program, uses chooseMessageFile implementationUploadFileType-
all v1.3.0All file types, only supported on WeChat Mini Program and H5UploadFileType-

file Data Structure

ParameterDescriptionTypeDefault Value
uidUnique identifier of current upload file in the listnumber-
thumbThumbnail addressstring-
nameCurrent file namestring-
statusCurrent file upload status, optional values are pending, loading, success, failUploadStatusType-
sizeFile sizenumber-
urlFile addressstring-
percentUpload progressnumber-
responseBackend returned content, may be string or objectstring | Record<string, any>-

Upload Slots

NameDescription
defaultCustom upload evoke area
preview-cover v1.3.12Custom content covering above the preview area, parameters are { file, index }

Upload Events

Event NameDescriptionParameters
successTriggered when upload succeedsUploadSuccessEvent
failTriggered when upload failsUploadErrorEvent
progressTriggered during uploadUploadProgressEvent
oversizeTriggered when file size exceeds limitUploadOversizeEvent
chooseerrorTriggered when file selection failsany
changeTriggered when upload list changesUploadChangeEvent
removeTriggered when file is removedUploadRemoveEvent
update:fileList v1.3.8Update event corresponding to v-model:file-listUploadFileItem[]

Upload Methods

Method NameDescriptionParameters
submit v1.3.8Manually start upload-
abortCancel upload(task?: UniApp.UploadTask) => void

主题定制

CSS 变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 全局配置

名称默认值描述
--wot-upload-size$n-88上传项尺寸
--wot-upload-radiusvar(--wot-upload-border-radius, $radius-main)上传项圆角
--wot-upload-border-color$border-main上传项边框颜色
--wot-upload-evoke-icon-size$n-32唤起项的图标大小
--wot-upload-evoke-bg$filled-bottom唤起项背景色
--wot-upload-evoke-color$text-auxiliary唤起项文字颜色
--wot-upload-evoke-font-size$typography-body-size-main唤起项文字大小
--wot-upload-evoke-line-height$typography-body-line--height-size-main唤起项文字行高
--wot-upload-evoke-color-disabled$text-disabled唤起项禁用文字颜色
--wot-upload-evoke-icon-color$icon-auxiliary唤起项图标颜色
--wot-upload-evoke-icon-color-disabled$icon-disabled唤起项禁用图标颜色
--wot-upload-close-padding$padding-ultra-tight关闭按钮内边距
--wot-upload-close-radiusvar(--wot-upload-close-border-radius, $radius-zero $radius-zero $radius-zero $radius-main)关闭按钮圆角
--wot-upload-close-bg$opacfilled-main-cover关闭按钮背景色
--wot-upload-close-icon-size$n-16关闭按钮图标尺寸
--wot-upload-close-icon-color$icon-white关闭按钮图标颜色
--wot-upload-progress-font-size$typography-body-size-main进度文字字号
--wot-upload-progress-line-height$typography-body-line--height-size-main进度文字行高
--wot-upload-progress-color$text-white进度文字颜色
--wot-upload-file-font-size$typography-label-size-main文件名文字字号
--wot-upload-file-line-height$typography-label-line--height-size-main文件名文字行高
--wot-upload-file-color$text-secondary文件名文字颜色
--wot-upload-file-padding$padding-zero $padding-tight文件/视频名称内边距
--wot-upload-preview-icon-size$n-24预览态内部图标尺寸
--wot-upload-preview-icon-color$icon-white预览态内部图标颜色
--wot-upload-preview-margin$spacing-zero $spacing-tight $spacing-tight $spacing-zero预览项外边距
--wot-upload-cover-icon-size$n-24视频/文件图标尺寸
--wot-upload-video-icon-size$n-24视频图标尺寸
--wot-upload-video-icon-color$icon-white视频播放图标颜色
--wot-upload-mask-bg$opacfilled-main-cover遮罩层背景色
--wot-upload-row-spacing$spacing-tight上传项行间距

Source Code

Documentation
Component

Released under the MIT License.

Released under the MIT License.