更新记录

1.0.0(2025-11-10) 下载此版本

1、基础文件选择功能


平台兼容性

uni-app x(4.83)

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

m-choose-file

uni-app UTS 插件,用于在 APP 平台选择文件。

平台支持

  • ✅ Android
  • ✅ iOS
  • ✅ HarmonyOS
  • ❌ 其他平台(会返回错误)

安装

将插件放置在 uni_modules 目录下即可。

使用方法

导入插件

import { onChooseFile } from '@/uni_modules/m-choose-file'

基本用法

// 选择单个文件
onChooseFile({
  maxSelectNumber: 1
})
  .then((res) => {
    if(res.files.length){
      console.log('选择的文件:', res.files)
    }
  })
  .catch((err) => {
    console.error('选择失败:', err)
  })

选择多个文件

// 选择多个文件(最多5个)
onChooseFile({
  maxSelectNumber: 5
})
  .then((res) => {
    if(res.files.length){
      console.log('选择的文件:', res.files)
    }
  })
  .catch((err) => {
    console.error('选择失败:', err)
  })

参数说明

参数 类型 必填 默认值 说明
maxSelectNumber number 1 最大选择文件数量

返回值

{
  files: [
    {
      tempFilePath: string,  // 文件临时路径
      name: string,          // 文件名
      type: string,          // 文件 MIME 类型
      size: number           // 文件大小(字节)
    }
  ]
}

错误码

错误码 说明
9010001 文件选择失败
9010003 不支持该平台

完整示例

<template>
  <button @click="chooseFile">选择文件</button>
</template>

<script setup>
import { onChooseFile } from '@/uni_modules/m-choose-file'

const chooseFile = () => {
  onChooseFile({
    maxSelectNumber: 1
  })
    .then((res) => {
      if(res.files.length){
        console.log('选择的文件:', res.files)
        uni.showToast({ title: '选择成功', icon: 'success' })
      }
    })
    .catch((err) => {
      console.error('选择失败:', err)
      uni.showToast({ title: '选择失败', icon: 'none' })
    })
}
</script>

开发文档

隐私、权限声明

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

存储权限、文件选择权限

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

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

许可协议

MIT协议