更新记录
1.0.2(2025-11-14) 下载此版本
1、增加根据文件类型筛选功能,不同平台存在差异
1.0.1(2025-11-14) 下载此版本
1、修复鸿蒙报错问题
1.0.0(2025-11-10) 下载此版本
1、基础文件选择功能
查看更多平台兼容性
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)
})
按文件类型过滤
// 只选择图片文件
onChooseFile({
maxSelectNumber: 1,
fileTypes: ['image']
})
.then((res) => {
if(res.files.length){
console.log('选择的图片:', res.files)
}
})
.catch((err) => {
console.error('选择失败:', err)
})
// 选择图片或视频
onChooseFile({
maxSelectNumber: 5,
fileTypes: ['image', 'video']
})
.then((res) => {
if(res.files.length){
console.log('选择的文件:', res.files)
}
})
.catch((err) => {
console.error('选择失败:', err)
})
// 选择文档文件
onChooseFile({
maxSelectNumber: 1,
fileTypes: ['doc']
})
.then((res) => {
if(res.files.length){
console.log('选择的文档:', res.files)
}
})
.catch((err) => {
console.error('选择失败:', err)
})
参数说明
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| maxSelectNumber | number | 否 | 1 | 最大选择文件数量 |
| fileTypes | string[] | 否 | ['all'] | 文件类型过滤,支持的类型见下方说明 |
fileTypes 支持的类型
| 类型值 | 说明 | 包含的文件类型 |
|---|---|---|
| all | 所有文件(默认) | 不限制文件类型 |
| image | 图片 | jpg, jpeg, png, gif, bmp, webp, svg, ico 等 |
| video | 视频 | mp4, avi, mov, wmv, flv, mkv, webm, m4v, 3gp 等 |
| audio | 音频 | mp3, wav, aac, flac, ogg, m4a, wma 等 |
| file | 文档文件 | 包含 doc、text、archive 的所有类型 |
| doc | 文档 | PDF、Word (.doc, .docx)、Excel (.xls, .xlsx)、PowerPoint (.ppt, .pptx) |
| text | 文本文件 | txt, rtf, json, xml, js, ts, java, cpp, c, h, py, html, css, md 等 |
| archive | 压缩包 | zip, rar, 7z, tar, gz 等 |
注意:
fileTypes可以传入多个类型,例如['image', 'video']表示同时支持图片和视频- 当传入
['all']或不传入fileTypes时,不限制文件类型 - 针对ios/harmony/android的文件限制代码结构不一样,选择限制可能存在偏差
返回值
{
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>

收藏人数:
下载插件并导入HBuilderX
赞赏(0)
下载 50
赞赏 0
下载 12589697
赞赏 1830
赞赏
京公网安备:11010802035340号