x-chooseFile-s 文件选择
按 type 选择图片、视频或任意文件。五端接口对齐,无需额外申请相册/存储权限。
兼容性
| Harmony | IOS | Android | WEB | 小程序 |
|---|---|---|---|---|
| 支持 | 支持 | 支持 | 支持 | 支持 |
调用
ts
import { chooseFile } from "@/uni_modules/x-chooseFile-s"
chooseFile({
type: "image",
count: 3,
success: (res) => {
console.log(res.tempFilePaths)
res.tempFiles.forEach((f) => {
console.log(f.name, f.size, f.fileType, f.mimeType, f.path)
})
},
fail: (err) => {
console.log(err.errCode, err.errMsg)
}
})方法
| 名称 | 需要 | 说明 |
|---|---|---|
| chooseFile | type?、count?、extension?、success | 打开系统选择器,成功返回 tempFilePaths / tempFiles |
参数
XChooseFileOptions
| 字段 | 说明 | 默认 |
|---|---|---|
| type | image / video / file / all。image、video 走相册;file、all 走文件选择器 | all |
| count | 最多可选。Android 文件选择器无法硬限制,超出截断 | 9 |
| extension | 扩展名过滤,带不带点均可。仅 file / all;微信仅 file | - |
| success | 成功 | - |
| fail | 失败 | - |
| complete | 结束 | - |
XChooseFileResult
| 字段 | 说明 |
|---|---|
| tempFilePaths | 临时路径列表 |
| tempFiles | 文件详情 |
XChooseFileTempFile
| 字段 | 说明 |
|---|---|
| path | 临时路径。Android 为 file:// + getCacheDir() 绝对路径(蒸汽模式 unifile://cache 也指向这里,不要写 getExternalCacheDir());iOS 为 unifile://cache/x-choosefile/...;Web 为 blob;鸿蒙为带临时授权的 uri |
| name | 文件名 |
| size | 字节数,未知为 0 |
| fileType | image / video / file |
| mimeType | MIME,未知为空 |
错误码:1001 系统错误,1002 参数错误,1004 权限拒绝,1005 正在选择,1006 用户取消,1008 平台不支持。
平台差异
type=all:App / Web 打开系统文件选择器;微信从聊天会话选文件。- iOS 12–13:图片/视频一次只能选 1 个。选中的文件拷到
Library/Caches/x-choosefile/,路径为unifile://cache/x-choosefile/...,给uni.getFileSystemManager()用。不要再把tmp绝对路径丢给readFile,iOS 会报「文件路径没有这个文件」。 - Web 取消依赖浏览器
cancel事件(Chrome 113+ / Safari 16.4+),旧浏览器取消可能无回调。
版本
版权归https://xui.tmui.design你不得修改及二次开发,仅供TMUI4会员商用使用。不得转给非VIP会员使用,一经查实数倍赔偿,并追究法律责任。
