翻譯進行中
本頁尚未翻譯為繁體中文,以下顯示簡體中文原文。範例的預設元件文字仍使用繁體中文。
Upload 上传
串行校验、受限并发、重试与替换上传;模型只保存成功后的远程文件。
引入
uni-app 配置 easycom 后使用 hui-upload。普通 Vue 从 @nscode/handy-uni/web 具名导入 HuiUpload,或安装预览插件。
示例
交互与事件结果
操作组件后,下方显示最近一次事件及完整参数。弹层选择器先修改草稿,确认后提交;取消保留原值。
vue
<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: `确定移除“${item.file.name ?? '此文件'}”吗?这会将文件从当前列表移除。`
})
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>
行为与平台差异
组件不内置删除确认框;未传 beforeRemove 时直接移除。业务需要二次确认时按示例在钩子里执行,取消、关闭、拒绝返回 false。等待期间列表或目标变化会作废钩子结果。Web 临时文件在拒绝、移除、替换和卸载时回收。
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
model-value | UniRemoteFile[] | undefined | undefined | 受控提交值;父组件不回写时展示保持原值。 |
default-value | UniRemoteFile[] | undefined | [] | 非受控初始值。 |
accept | UploadAccept | undefined | 'image' | 文件类别 image/video/media/file/all。 |
limit | number | undefined | 0 | 文件总数上限,0 不限;已成功和排队任务均计数。 |
multiple | boolean | undefined | true | 允许多选。 |
max-size | number | ((file: UniLocalFile) => boolean) | undefined | — | 文件大小字节上限或逐文件校验函数。 |
extensions | string[] | undefined | — | 允许扩展名,不区分大小写。 |
source-type | ("album" | "camera")[] | undefined | — | 选择来源 album/camera。 |
size-type | ("original" | "compressed")[] | undefined | — | 图片 original/compressed。 |
camera | "back" | "front" | undefined | — | 视频摄像头 front/back。 |
max-duration | number | undefined | — | 视频最长秒数。 |
uploader | UniUploader | undefined | — | 上传器接收 file/onProgress/signal,返回远程文件。 |
concurrency | number | undefined | 3 | 同时上传数,最少为 1。 |
before-upload | ((file: UniLocalFile) => boolean | void | UniLocalFile | Promise<boolean | void | UniLocalFile>) | undefined | — | 上传前校验,可返回替换文件;false、抛错、拒绝拦截。 |
before-remove | ((file: UploadListItem, index: number) => boolean | void | Promise<boolean | void>) | undefined | — | 移除前钩子;false、抛错或拒绝拦截;未传时直接移除。 |
list-type | "picture-card" | "picture" | "text" | undefined | — | picture-card 卡片、picture 缩略行、text 文件名行。 |
preview-size | number | undefined | 80 | 卡片宽高 px。 |
previewable | boolean | undefined | true | 点击文件时允许预览。 |
reupload | boolean | undefined | false | 点击成功项后重新选择,成功时原位替换。 |
deletable | boolean | undefined | true | 显示删除入口。 |
disabled | boolean | undefined | false | 禁用用户操作;优先于表单继承配置。 |
readonly | boolean | undefined | undefined | 只读;未设置时继承表单上下文。 |
size | ComponentSize | undefined | — | 尺寸;属性 → FormItem → Form → ConfigProvider,取首个已设置值。 |
upload-text | string | undefined | — | 添加按钮文字。 |
custom-class | string | undefined | '' | 根节点附加类名。 |
custom-style | string | CSSProperties | undefined | — | 根节点附加样式。 |
Events
| 事件 | 参数 | 说明 |
|---|---|---|
update:modelValue | (files: UniRemoteFile[]) | 请求更新 modelValue;受控模式由父级回写。 |
change | (files: UniRemoteFile[], context: { action: "add" | "remove" | "replace"; file: UniRemoteFile; }) | 提交后的值与上下文,发生在模型更新事件之后。 |
success | (file: UniRemoteFile, task: UploadTask) | 报告 success,完整参数见示例事件输出。 |
error | (error: unknown, task: UploadTask) | 报告 error,完整参数见示例事件输出。 |
exceed | (files: UniLocalFile[]) | 报告 exceed,完整参数见示例事件输出。 |
reject | (file: UniLocalFile, reason: "type" | "size" | "before-upload") | 报告 reject,完整参数见示例事件输出。 |
remove | (item: UploadListItem) | 报告 remove,完整参数见示例事件输出。 |
preview | (item: UploadListItem, context: { index: number; }) | 报告 preview,完整参数见示例事件输出。 |
uploading-change | (uploading: boolean) | 报告 uploading-change,完整参数见示例事件输出。 |
choose-error | (error: HuiPlatformError) | 报告 choose-error,完整参数见示例事件输出。 |
Slots
| 插槽 | 说明 |
|---|---|
item | 上传列表项,作用域 {item,index,remove,retry,preview}。 |
default | 默认内容。 |
tip | 上传提示内容。 |
方法
| 方法 | 签名 |
|---|---|
choose | () => Promise<void> |
retryAll | () => void |
abort | (uid?: string | undefined) => void |
类型
UploadProps、UploadEmits、UploadExpose。公开类型由包入口统一导出。
平台说明
| 范围 | 行为与验证边界 |
|---|---|
| 公共实现 | 通过平台选择文件并交给 useUpload/createUploader;上传接口和响应解析由业务提供。 |
| 设备待验 | 微信、App、H5 分别核对媒体/文件选择能力、权限取消、进度、重传、删除确认与临时文件释放;浏览器不使用本机路径上传。 |
对应操作见 examples/uni-demo/smoke/Upload.md。单元测试不替代真机验收。