更新记录

1.0.0(2026-08-13) 下载此版本

初始版本


平台兼容性

uni-app(4.41)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
- - - - - - - - -
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 小红书小程序 快应用-华为 快应用-联盟
- - - - - - - - - - - -

uni-app x(4.81)

Chrome Safari Android iOS 鸿蒙 微信小程序
- - - - - -

scx-choosefile

uni-app 文件选择插件,同时支持 uniapp 和 uniapp x,覆盖 Android、iOS、鸿蒙 next、Web 和微信小程序。

功能特性

  • 同时支持 uniapp 与 uniapp x 双框架
  • 覆盖 H5 / 微信小程序 / Android / iOS / 鸿蒙 next 五端
  • 统一 API 调用方式 scx.chooseFile
  • 同时支持 Promise 与回调两种调用方式
  • 支持文件类型过滤(image/video/audio/all)
  • 支持文件后缀名限制
  • 支持多选和单选模式
  • 支持相册/相机来源指定(image/video,部分平台)

平台兼容性

平台 实现方式 sourceType 支持 备注
H5 原生 <input type="file"> image/video 支持 通过 capture 属性控制相机
微信小程序 wx.chooseMessageFile 不支持 仅能选择微信会话中的文件
Android(uniapp) Intent.ACTION_GET_CONTENT 不支持 通过 SAF 访问,无需存储权限
iOS(uniapp) UIDocumentPickerViewController / uni.chooseImage / uni.chooseVideo image/video 支持 图片/视频走相册 API
Android(uniapp x) Intent.ACTION_GET_CONTENT 不支持 通过 SAF 访问
iOS(uniapp x) UIDocumentPickerViewController 不支持 图片/视频走 Files App
鸿蒙 next filePicker.select 不支持 文件选择器

注:uniapp 项目走 js_sdk/ 实现,uniapp x 项目走 utssdk/ 实现,由 HBuilderX 自动选择。

安装方式

方式一:插件市场安装

在 DCloud 插件市场搜索 scx-choosefile,点击安装即可。

方式二:手动安装

uni_modules/scx-choosefile 目录复制到项目的 uni_modules 目录下。

使用方法

基础用法

import scx from '@/uni_modules/scx-choosefile/js_sdk/index.js'

// Promise 方式
async function chooseFile() {
  try {
    const result = await scx.chooseFile({
      count: 10,
      type: 'all'
    })
    console.log('选择成功:', result.tempFiles)
  } catch (err) {
    console.error('选择失败:', err)
  }
}

// 回调方式
scx.chooseFile({
  count: 10,
  type: 'all',
  success: (result) => {
    console.log('选择成功:', result.tempFiles)
  },
  fail: (err) => {
    console.error('选择失败:', err)
  }
})

uniapp x 项目

uniapp x 项目可直接通过 uni.requireNativePlugin 或导入 utssdk 入口使用:

import { chooseFile } from '@/uni_modules/scx-choosefile/utssdk/chooseFile.uts'

chooseFile({ count: 10, type: 'all' }).then(res => {
  console.log(res.tempFiles)
})

参数说明

参数 类型 必填 默认值 说明
count number 100 最多可以选择的文件数
type string 'all' 文件类型,可选值:'image' / 'video' / 'audio' / 'all'
extension string[] [] 文件后缀名限制,如 ['pdf', 'doc', 'docx'],与 type 同时存在时以 extension 优先
sourceType Array<'album' | 'camera'> [] 图片/视频来源,仅 typeimage/video 时生效;空数组或不传表示使用平台默认行为
success function - 成功回调
fail function - 失败回调
complete function - 完成回调(成功/失败都会执行)

sourceType 平台支持见上方"平台兼容性"表格,不支持的平台会静默忽略。

返回值说明

成功时返回 ChooseFileSuccess 对象:

字段 类型 说明
tempFilePaths string[] 文件的本地路径列表
tempFiles ChooseFileTempFile[] 文件信息列表

ChooseFileTempFile 字段:

字段 类型 说明
name string 文件名
path string 文件路径
size number 文件大小(字节);部分平台(Android/iOS 原生)可能返回 0
type string 文件类型:'image' / 'video' / 'audio' / 'file'

失败时返回 ChooseFileFail 对象:

字段 类型 说明
errCode number 错误码
errMsg string 错误信息

错误码说明

错误码 含义 触发场景
1101001 用户取消 用户主动取消选择
1101005 权限被拒 Android 端(uniapp 路径)用户拒绝存储权限
1101006 文件类型不匹配 选择结果经过 extension/类型过滤后无任何文件
1101010 其他错误 环境不支持、解析失败、并发调用等

完整示例

import scx from '@/uni_modules/scx-choosefile/js_sdk/index.js'

// 选择图片(仅相册,不弹拍摄选项)
async function chooseImage() {
  const result = await scx.chooseFile({
    count: 9,
    type: 'image',
    sourceType: ['album']
  })
  console.log('选择了', result.tempFiles.length, '张图片')
}

// 选择视频(仅相册)
async function chooseVideo() {
  const result = await scx.chooseFile({
    count: 5,
    type: 'video',
    sourceType: ['album']
  })
  console.log('选择了', result.tempFiles.length, '个视频')
}

// 选择指定格式文件
async function chooseDocument() {
  const result = await scx.chooseFile({
    count: 5,
    type: 'all',
    extension: ['pdf', 'doc', 'docx', 'xlsx']
  })
  console.log('选择了', result.tempFiles.length, '个文档')
}

// 单选文件
async function chooseSingle() {
  const result = await scx.chooseFile({
    count: 1,
    type: 'all'
  })
  console.log('选择了:', result.tempFiles[0].name)
}

注意事项

  1. 微信小程序限制:微信小程序端使用 wx.chooseMessageFile 实现,仅能选择微信会话中的文件,无法访问手机本地文件系统。sourceType 在该端不生效。

  2. Android 权限:Android 端使用系统文件选择器(SAF / ACTION_GET_CONTENT),无需在 manifest.json 中声明 READ_EXTERNAL_STORAGE 权限。仅在 uniapp(plus.android 路径)下会主动申请该权限作为兼容性兜底,可被拒绝而不影响选择。

  3. iOS 权限:iOS 端使用 UIDocumentPickerViewController,无需在 Info.plist 中添加额外权限说明。如果通过 uni.chooseImage/uni.chooseVideo 走相册路径,则需配置 NSPhotoLibraryUsageDescription

  4. 鸿蒙权限:鸿蒙端使用 filePicker.select,无需在 module.json5 中配置文件访问权限。

  5. 文件路径格式:各平台返回的 path 格式不同:

    • H5:blob: 协议
    • 微信小程序:wxfile:// 协议
    • Android(uniapp / uniapp x):通常为 content:// 协议
    • iOS:通常为 file:// 协议
    • 鸿蒙:通常为 file://docs/... URI
  6. 文件大小:原生实现(Android UTS、iOS UTS、plus.android)当前返回 size: 0,如需精确大小请通过 uni.getFileInfo 等接口查询。

  7. 并发调用:UTS Android/iOS 实现做了单例保护,上一次选择未结束前再次调用会立即返回 errCode: 1101010

技术架构

                        scx.chooseFile
                              │
        ┌─────────────────────┼─────────────────────┐
        ▼                     ▼                     ▼
  uniapp 项目              uniapp x 项目       微信小程序 / H5
  (js_sdk/)               (utssdk/)           (js_sdk/)
        │                     │
   ┌────┴────┐          ┌─────┼─────┐
   ▼         ▼          ▼     ▼     ▼
 Android   iOS        Android iOS   鸿蒙
 (plus     (uni.     (UTS    (UTS  (UTS
  .android) choose*)  原生)   原生)  原生)

目录结构

uni_modules/scx-choosefile/
├── js_sdk/                         # uniapp / 小程序 / H5 端入口
│   ├── platform/
│   │   ├── h5.js                   # H5 实现
│   │   ├── mp-weixin.js            # 微信小程序实现
│   │   ├── app.js                  # App Android(plus.android)实现
│   │   ├── app-ios.js              # App iOS(uni.chooseImage/Video + plus.io)实现
│   │   └── app-harmony.js          # 鸿蒙 next(uni.chooseFile)实现
│   ├── index.js                    # 入口(条件编译分发)
│   └── index.d.ts                  # TypeScript 类型定义
├── utssdk/                         # uniapp x 端入口
│   ├── app-android/
│   │   └── index.uts               # Android 原生实现(Intent + SAF)
│   ├── app-ios/
│   │   └── index.uts               # iOS 原生实现(UIDocumentPickerViewController)
│   ├── app-harmony/
│   │   └── index.uts               # 鸿蒙原生实现(filePicker)
│   ├── chooseFile.uts              # UTS 入口(条件编译分发)
│   ├── interface.uts               # UTS 类型定义
│   └── config.json                 # UTS 插件配置
├── package.json                    # 插件配置
├── platform.json                   # 平台支持声明
└── README.md                       # 使用文档

更新日志

v1.0.0

  • 初始版本
  • 支持 H5、微信小程序、Android、iOS、鸿蒙 next 平台
  • 提供统一的 scx.chooseFile API

许可证

MIT License

隐私、权限声明

1. 本插件需要申请的系统权限列表:

需要访问相册和文件存储权限用于选择图片和文件

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT协议

暂无用户评论。