Upload
Serialized validation, bounded concurrency, retry and replacement upload. Only completed remote files enter the model.
Import
With uni-app easycom, use hui-upload。 Standalone Vue imports @nscode/handy-uni/web HuiUpload or installs the preview plugin.
Examples
Interaction and event results
Interact with the control. The latest event and its full payload are shown below. Popup selectors commit only after confirmation; cancellation keeps the previous value.
<script setup lang="ts">
import { ref } from 'vue'
import { showConfirmDialog } from '@nscode/handy-uni'
import type { UniUploader, UploadListItem } from '@nscode/handy-uni'
const result = ref('')
const files = ref([])
const uploader: UniUploader = async ({ file, onProgress }) => {
onProgress(100)
return { id: file.path, url: 'https://picsum.photos/200/200', name: file.name, type: file.type }
}
async function beforeRemove(item: UploadListItem) {
try {
await showConfirmDialog({
message: `Remove “${item.file.name ?? 'this file'}” from the current list?`
})
return true
} catch {
return false
}
}
function event0(arg0: unknown) {
result.value = 'update:modelValue' + ': ' + JSON.stringify([arg0])
}
function event1(arg0: unknown, arg1: unknown) {
result.value = 'change' + ': ' + JSON.stringify([arg0, arg1])
}
function event2(arg0: unknown, arg1: unknown) {
result.value = 'success' + ': ' + JSON.stringify([arg0, arg1])
}
function event3(arg0: unknown, arg1: unknown) {
result.value = 'error' + ': ' + JSON.stringify([arg0, arg1])
}
function event4(arg0: unknown) {
result.value = 'exceed' + ': ' + JSON.stringify([arg0])
}
function event5(arg0: unknown, arg1: unknown) {
result.value = 'reject' + ': ' + JSON.stringify([arg0, arg1])
}
function event6(arg0: unknown) {
result.value = 'remove' + ': ' + JSON.stringify([arg0])
}
function event7(arg0: unknown, arg1: unknown) {
result.value = 'preview' + ': ' + JSON.stringify([arg0, arg1])
}
function event8(arg0: unknown) {
result.value = 'uploading-change' + ': ' + JSON.stringify([arg0])
}
function event9(arg0: unknown) {
result.value = 'choose-error' + ': ' + JSON.stringify([arg0])
}
</script>
<template>
<view>
<hui-page-host />
<hui-upload
v-model="files"
:uploader="uploader"
:limit="3"
:concurrency="2"
:before-remove="beforeRemove"
@update:model-value="event0"
@change="event1"
@success="event2"
@error="event3"
@exceed="event4"
@reject="event5"
@remove="event6"
@preview="event7"
@uploading-change="event8"
@choose-error="event9"
><template #tip
><text>最多三张;示例上传器不发网络请求 / Three files, local mock uploader</text></template
></hui-upload
>
<text aria-live="polite">{{ result }}</text>
</view>
</template>
Behavior and platform support
The component does not display a removal confirmation. Without beforeRemove, removal proceeds directly. Applications can confirm through the hook as shown above and return false on cancellation, dismissal or rejection. A changed collection or target invalidates a pending hook result. Temporary web files are released on rejection, removal, replacement and disposal.
Props
| Property | Type | Default | Description |
|---|---|---|---|
model-value | UniRemoteFile[] | undefined | undefined | Controlled committed value; display remains unchanged until the parent writes back. |
default-value | UniRemoteFile[] | undefined | [] | Initial uncontrolled value. |
accept | UploadAccept | undefined | 'image' | Accepted file kind: image, video, media, file or all. |
limit | number | undefined | 0 | Total file limit; zero is unlimited. Completed and queued files both count. |
multiple | boolean | undefined | true | Allow multiple selections. |
max-size | number | ((file: UniLocalFile) => boolean) | undefined | — | Maximum bytes per file or a per-file validator. |
extensions | string[] | undefined | — | Allowed extensions, case insensitive. |
source-type | ("album" | "camera")[] | undefined | — | Allowed sources: album and camera. |
size-type | ("original" | "compressed")[] | undefined | — | Original or compressed image size. |
camera | "back" | "front" | undefined | — | Video camera, front or back. |
max-duration | number | undefined | — | Maximum video duration in seconds. |
uploader | UniUploader | undefined | — | Uploader receiving file/onProgress/signal and returning a remote file. |
concurrency | number | undefined | 3 | Concurrent upload count, minimum one. |
before-upload | ((file: UniLocalFile) => boolean | void | UniLocalFile | Promise<boolean | void | UniLocalFile>) | undefined | — | Validate or replace a file; false, throw and rejection reject it. |
before-remove | ((file: UploadListItem, index: number) => boolean | void | Promise<boolean | void>) | undefined | — | Removal guard; false, throw or rejection cancels. Without it, removal proceeds directly. |
list-type | "picture-card" | "picture" | "text" | undefined | — | picture-card tiles, picture rows or text filename rows. |
preview-size | number | undefined | 80 | Tile size in pixels. |
previewable | boolean | undefined | true | Allow preview on file click. |
reupload | boolean | undefined | false | Choose a replacement on a completed item and replace in place after success. |
deletable | boolean | undefined | true | Show removal actions. |
disabled | boolean | undefined | false | Disable user interaction, including inherited form state. |
readonly | boolean | undefined | undefined | Readonly; inherit form context when omitted. |
size | ComponentSize | undefined | — | Size priority: prop → FormItem → Form → ConfigProvider. |
upload-text | string | undefined | — | Add-file button text. |
custom-class | string | undefined | '' | Additional root classes. |
custom-style | string | CSSProperties | undefined | — | Additional root styles. |
Events
| Event | Parameters | Description |
|---|---|---|
update:modelValue | (files: UniRemoteFile[]) | Requests a controlled value update. |
change | (files: UniRemoteFile[], context: { action: "add" | "remove" | "replace"; file: UniRemoteFile; }) | Reports change with the arguments shown in the example. |
success | (file: UniRemoteFile, task: UploadTask) | Reports success with the arguments shown in the example. |
error | (error: unknown, task: UploadTask) | Reports error with the arguments shown in the example. |
exceed | (files: UniLocalFile[]) | Reports exceed with the arguments shown in the example. |
reject | (file: UniLocalFile, reason: "type" | "size" | "before-upload") | Reports reject with the arguments shown in the example. |
remove | (item: UploadListItem) | Reports remove with the arguments shown in the example. |
preview | (item: UploadListItem, context: { index: number; }) | Reports preview with the arguments shown in the example. |
uploading-change | (uploading: boolean) | Reports uploading-change with the arguments shown in the example. |
choose-error | (error: HuiPlatformError) | Reports choose-error with the arguments shown in the example. |
Slots
| Slot | Description |
|---|---|
item | Upload item scope: {item,index,remove,retry,preview}. |
default | Default content. |
tip | Upload helper content. |
Methods
| Method | Signature |
|---|---|
choose | () => Promise<void> |
retryAll | () => void |
abort | (uid?: string | undefined) => void |
Types
UploadProps、UploadEmits、UploadExpose。 Public types are exported from the package entry.
Platform notes
| Scope | Behavior and validation limits |
|---|---|
| Shared implementation | Uses platform file selection and useUpload/createUploader. The business supplies the upload endpoint and response parser. |
| Device checks pending | Check file/media capabilities, permission cancellation, progress, retries, removal confirmation and temporary-file cleanup on each target; browsers cannot upload by local path. |
See examples/uni-demo/smoke/Upload.md for the checklist. Unit tests do not replace device acceptance.