UploadFile 文件上传
基础用法
以纵向列表管理附件选择与上传,适用于资料提交、工单附件和文档收集。
- 按文件扩展名展示图标,显示名称、大小、上传进度和状态
- 支持多选、类型与大小校验、删除和失败重试
- 支持 HTTP 与 uniCloud 上传,可自动或手动执行上传队列
<template>
<sn-upload-file v-model="files" url="https://example.com/upload" />
</template>
<script lang="ts" setup>
import type { SnUploadFile } from '@/uni_modules/sinle-ui/index.ts'
const files = ref<SnUploadFile[]>([])
</script>更多演示请下载 demo 查看
限制文件
<sn-upload-file v-model="files" :max-count="5" :max-size="10 * 1024 * 1024"
:accept="['.pdf', '.docx', '.xlsx']" :auto-upload="false"
@oversize="onOversize" @invalid-type="onInvalidType" @exceed="onExceed" />文件入列前先校验扩展名和大小,再截取数量上限以内的有效文件;被拒绝的文件不进入列表,也不占用名额。accept 为扩展名数组,可省略前导点,不区分大小写,支持 .tar.gz 等复合后缀,不接收 MIME 类型。
组件仅通过 choose-error、invalid-type、oversize、exceed 事件通知选择异常,不显示错误文本或弹出提示。调用方自行决定反馈方式;Demo 在这些事件回调中统一调用全局 snu.showToast。
<template>
<sn-upload-file :auto-upload="false" :accept="['.pdf']" @invalid-type="onInvalidType" @choose-error="onChooseError" />
</template>
<script lang="ts" setup>
import { snu } from '@/uni_modules/sinle-ui/index.ts'
import type { SnUploadFile } from '@/uni_modules/sinle-ui/index.ts'
function onInvalidType(file: SnUploadFile): void {
snu.showToast({ text: `${file.name} 的文件类型不符合要求` })
}
function onChooseError(error: UniError): void {
snu.showToast({ text: error.cause?.message ?? error.errMsg })
}
</script>手动上传
<template>
<sn-upload-file ref="uploader" v-model="files" :url="uploadUrl" :auto-upload="false"
name="attachment" :form-data="{ scene: 'ticket' }" :timeout="15000"
@uploaded="onUploaded" @upload-error="onUploadError" />
<sn-button text="上传附件" @click="uploader?.upload()" />
</template>
<script lang="ts" setup>
import { snu } from '@/uni_modules/sinle-ui/index.ts'
import type { SnUploadFile, SnUploadFileResponse, SnUploadFileInstance } from '@/uni_modules/sinle-ui/index.ts'
const uploader = ref<SnUploadFileInstance | null>(null)
const files = ref<SnUploadFile[]>([])
const uploadUrl = 'https://example.com/upload'
function onUploaded(_response: SnUploadFileResponse, file: SnUploadFile): void {
snu.showToast({ text: `${file.name} 上传成功` })
}
function onUploadError(error: UniError, _file: SnUploadFile | null): void {
snu.showToast({ text: error.cause?.message ?? error.errMsg })
}
</script>upload() 将未上传与失败条目加入串行队列;重复调用不会重复排队。retry(id) 仅重试指定的失败条目。HTTP 模式的成功响应包含 data、statusCode,业务方按接口约定解析 data。
uniCloud 上传
<template>
<sn-upload-file v-model="files" upload-mode="unicloud" directory="demoUpload/files/"
@uploaded="onUploaded" @upload-error="onUploadError" />
</template>
<script lang="ts" setup>
import { snu } from '@/uni_modules/sinle-ui/index.ts'
import type { SnUploadFile, SnUploadFileResponse } from '@/uni_modules/sinle-ui/index.ts'
const files = ref<SnUploadFile[]>([])
const lastFileID = ref('')
function onUploaded(response: SnUploadFileResponse, file: SnUploadFile): void {
if ('fileID' in response) lastFileID.value = response.fileID
snu.showToast({ text: `${file.name} 上传成功` })
}
function onUploadError(error: UniError): void {
snu.showToast({ text: error.cause?.message ?? error.errMsg })
}
</script>直接调用当前项目默认关联空间的 uniCloud.uploadFile,不需要配置 HTTP 地址或自行初始化另一套空间。Demo 使用本项目已关联的 uniCloud,将文件保存到 demoUpload/files/。云端文件名使用随机标识并保留常规扩展名,避免同名文件互相覆盖;阿里云按实际路径存储,其他服务商使用各自的路径规则。
成功结果保存在 file.response,事件返回同一原始响应。以 fileID 关联业务附件,不依赖服务商可能缺省的 filePath;fileID 为云存储标识时可通过 uniCloud.getTempFileURL 获取访问链接。
自定义入口
<sn-upload-file v-model="files" :auto-upload="false">
<template #default="{ choose, disabled }">
<sn-button text="添加申请材料" icon="attachment-2" :disabled="disabled" @click="choose" />
</template>
</sn-upload-file>属性
| 参数 | 说明 | 类型 | 默认值 | 可选值 |
|---|---|---|---|---|
| v-model | 文件列表,条目 ID 必须唯一 | SnUploadFile[] | [] | - |
| uploadMode | 上传方式 | String | http | http | unicloud |
| directory | uniCloud 云端目录,自动规整首尾斜杠 | String | uploadFiles/ | - |
| url | HTTP 上传地址,为空时触发错误且不创建任务 | String | '' | - |
| name | HTTP multipart 文件表单字段名,与原始文件名无关 | String | file | - |
| header | HTTP 上传请求头,不要手动设置 multipart 的 Content-Type | Record<string, string> | {} | - |
| formData | HTTP 上传请求附加表单数据 | SnFormData | {} | - |
| autoUpload | 选择文件后自动上传本次新增条目 | Boolean | true | true | false |
| chooseCount | 一次最多选择的数量,最小为 1 | Number | 20 | - |
| maxCount | 列表最大文件数,小于等于 0 不限制 | Number | -1 | - |
| maxSize | 单文件大小上限,单位为字节,小于等于 0 不限制 | Number | -1 | - |
| accept | 允许的文件扩展名,为空时不限制 | String[] | [] | - |
| timeout | 单文件等待上传结果的毫秒数,非正数或非有限值回退为 30000;各模式的超时行为见下文 | Number | 30000 | - |
| disabled | 禁止选择、删除和发起上传,当前任务继续,队列暂停至解除禁用 | Boolean | false | true | false |
| clearable | 是否显示删除按钮,不影响 remove 方法 | Boolean | true | true | false |
| chooseText | 选择按钮文字,空值使用当前语言文案 | String | 选择文件 | - |
| uploadingText | 上传中状态文字,空值使用当前语言文案 | String | 上传中 | - |
| uploadedText | 上传成功状态文字,空值使用当前语言文案 | String | 上传成功 | - |
| unuploadedText | 未上传状态文字,空值使用当前语言文案 | String | 未上传 | - |
| errorText | 上传失败状态文字,空值使用当前语言文案 | String | 上传失败 | - |
| itemBorder | 是否显示列表分隔线 | Boolean | true | true | false |
| itemClass | 透传至列表单元格的外部类 | String | '' | - |
| itemStyle | 列表单元格样式 | SnStyleValue | '' | - |
| itemTextStyle | 文件名称样式 | SnStyleValue | '' | - |
| itemIconStyle | 文件图标样式 | SnStyleValue | '' | - |
| itemValueStyle | 上传状态文字样式 | SnStyleValue | '' | - |
| customStyle | 上传区域根节点样式 | SnStyleValue | '' | - |
事件
| 名称 | 类型 | 说明 |
|---|---|---|
| file-chose | (files: SnUploadFile[]) => Void | 本次通过校验并加入列表的文件;用户取消不触发 |
| choose-error | (error: UniError) => Void | 文件选择失败,仅返回事件供调用方处理;用户取消不触发 |
| uploaded | (response: SnUploadFileResponse, file: SnUploadFile) => Void | 单文件上传成功,响应结构由 uploadMode 决定 |
| upload-error | (error: UniError, file: SnUploadFile | null) => Void | 单文件上传失败,或上传配置不可用;配置错误时 file 为 null |
| progress | (file: SnUploadFile) => Void | 文件上传进度变化 |
| delete | (file: SnUploadFile) => Void | 点击删除按钮或调用 remove 方法删除条目 |
| oversize | (file: SnUploadFile) => Void | 单文件超出大小限制 |
| invalid-type | (file: SnUploadFile) => Void | 单文件扩展名不符合 accept |
| exceed | (files: SnUploadFile[]) => Void | 本次选择中超过 chooseCount 或 maxCount 的有效文件 |
方法
| 名称 | 参数 | 返回值 | 描述 |
|---|---|---|---|
| choose | - | Void | 打开系统文件选择器,达到数量上限时不打开 |
| upload | - | Void | 将所有未上传和失败文件加入上传队列 |
| retry | id: String | Void | 重试指定 ID 的失败条目 |
| remove | id: String | Void | 删除指定 ID 的条目,并停止接收该次上传结果 |
上述方法均受 disabled 约束。也可通过替换 v-model 清空或移除条目。组件卸载时停止队列,将当前条目恢复为未上传;任务中止及资源释放时机遵循下方各模式的说明。
插槽
| 名称 | 说明 | 作用域参数 |
|---|---|---|
| default | 替换选择文件按钮,达到数量上限时隐藏 | choose 打开选择器的方法、disabled 禁用或正在选择 |
数据类型
从 @/uni_modules/sinle-ui/index.ts 导入 SnUploadFile、SnUploadFileResponse、SnUploadFileInstance。
文件上传条目字段,完整定义见组件类型。
回显附件时提供完整条目并设置 status: 'uploaded'、progress: 100。进度达到 100% 后仍需等待服务端响应才会变为成功状态。文件名过长时单行省略,图标按常见文档、表格、演示文稿、压缩包、图片、音视频的扩展名区分。
上传行为
HTTP 模式通过 uni.uploadFile 逐个执行,与 sn-upload-media 一致,仅 HTTP 200 视为成功。非 200 响应、网络失败、同步调用异常或任务结束却未返回结果,均会将条目设为失败并继续队列;接口中的业务失败码需由调用方自行识别。
HTTP 超时由请求参数和独立计时器共同保障,先落地失败状态再中止任务。Web HTTPS 页面上传 HTTP 地址被浏览器拦截时沿用媒体上传的超时处理;删除条目或卸载时同样中止 HTTP 任务。
云上传限制
uniCloud.uploadFile 不提供实际传输的中止句柄。超时会立即将条目设为失败并触发事件;删除、超时或卸载后的结果均被忽略,但云端文件仍可能上传成功。为保持串行及避免提前释放 Web 临时文件,后续队列与临时资源清理会等待当前上传 Promise 结束;删除列表条目不删除云端文件。
uniCloud 进度使用 onUploadProgress 的已发送和总字节数计算,Promise 成功且返回有效 fileID 后才标记为成功。同一任务只派发一次终态事件,所有模式都会忽略已失效任务的迟到回调。
错误码沿用上传组件:9010006 uniCloud 未初始化、9010007 未配置 HTTP 地址、9010009 上传失败、9010010 选择失败、9010016 上传超时;详情见 error.cause.message。
平台差异
使用官方 uni.chooseFile,Web 要求 HBuilderX 4.0+、Android 4.51+、iOS 和鸿蒙 4.61+。Android/iOS 的系统选择器不支持扩展名过滤,Android 的 count 仅区分单选与多选,因此所有平台都会在入列前再次校验类型、大小与数量。Android 的 content 协议路径直接传给上传 API。
Web 保留系统选择器返回的文件对象以携带原始文件名;原生端传本地路径。临时路径属于当前会话,不应持久化后用于跨会话续传。此组件不提供文件内容预览,也不删除服务端已上传的文件。

