更新记录
0.1.0(2026-06-27) 下载此版本
初次提交
平台兼容性
uni-app x(5.0)
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|
| × | × | 5.0 | 14 | 9 | - |
其他
| 多语言 | 暗黑模式 | 宽屏模式 |
|---|---|---|
| × | √ | √ |
laoqianjunzi-file
laoqianjunzi-file 是一个面向 uni-app x 的 UTS 文件能力插件,统一提供以下能力:
- 本地文件选择
- 本地文件上传
- 目录解析与目录遍历
- 文本文件读写
- 文件与目录的创建、复制、移动、删除
- 批量远程下载
- 压缩与解压
- 文件只读与隐藏状态控制
本插件以当前正式 API 为唯一文档入口,README 与示例页均不涉及旧插件、旧命名或兼容式调用说明。
适用平台
- Android
- iOS
- Harmony
- Web
要求环境:
- HBuilderX
5.07及以上 uni-app x工程
能力总览
平台能力矩阵
| 能力 | Android | iOS | Harmony | Web |
|---|---|---|---|---|
selectLocalFiles |
支持 | 支持 | 支持 | 支持 |
uploadLocalFile |
支持 | 支持 | 支持 | 支持 |
requestFileAccess |
支持 | 支持 | 支持 | 支持 |
describeCapabilities |
支持 | 支持 | 支持 | 支持 |
resolveDirectory |
支持 | 支持 | 支持 | 不支持 |
createEntry |
支持 | 支持 | 支持 | 不支持 |
copyEntry |
支持 | 支持 | 支持 | 不支持 |
moveEntry |
支持 | 支持 | 支持 | 不支持 |
deleteEntry |
支持 | 支持 | 支持 | 不支持 |
existsEntry |
支持 | 支持 | 支持 | 不支持 |
measureEntry |
支持 | 支持 | 支持 | 不支持 |
readTextContent |
支持 | 支持 | 支持 | 支持 |
writeTextContent |
支持 | 支持 | 支持 | 不支持 |
readDirectory |
支持 | 支持 | 支持 | 不支持 |
downloadRemoteFiles |
支持 | 支持 | 支持 | 不支持 |
updateReadOnlyState |
支持 | 不支持 | 不支持 | 不支持 |
updateHiddenState |
支持 | 不支持 | 不支持 | 不支持 |
compressDirectory |
支持 | 不支持 | 不支持 | 不支持 |
compressEntries |
支持 | 不支持 | 不支持 | 不支持 |
extractArchive |
支持 | 不支持 | 不支持 | 不支持 |
describeCapabilities() 返回值说明
describeCapabilities() 会返回当前平台声明支持的能力标签:
| 标签 | 含义 |
|---|---|
picker |
本地文件选择 |
upload |
本地文件上传 |
permission |
运行时权限申请或权限确认 |
archive |
压缩与解压 |
hidden |
文件隐藏状态控制 |
read-only |
文件只读状态控制 |
batch-download |
批量远程下载 |
public-storage |
公开存储目录访问 |
当前各端的典型返回:
- Android:
picker、upload、permission、archive、hidden、read-only、batch-download、public-storage - iOS:
picker、upload、permission、batch-download - Harmony:
picker、upload、permission、batch-download - Web:
picker、upload
安装与引入
将插件放入项目的 uni_modules 目录后,直接按模块路径引入:
import {
describeCapabilities,
requestFileAccess,
resolveDirectory,
selectLocalFiles,
writeTextContent
} from '@/uni_modules/laoqianjunzi-file'
内置示例页
插件内已提供演示页:
uni_modules/laoqianjunzi-file/pages/index.uvue
该页面演示了:
- 权限检查与能力读取
- 文件选择与结果展示
- 工作区目录创建
- 文本文件写入、追加、读取、列表刷新
- 文件复制、移动、删除
- 批量下载、上传、压缩、解压
快速开始
1. 检查平台能力与权限
import {
describeCapabilities,
requestFileAccess
} from '@/uni_modules/laoqianjunzi-file'
const capabilityResult = describeCapabilities()
console.log('capabilities', capabilityResult.capabilities)
requestFileAccess().then((permissionResult) => {
console.log('permission', permissionResult)
})
2. 选择文件
import {
selectLocalFiles,
type LaoFileRecord
} from '@/uni_modules/laoqianjunzi-file'
selectLocalFiles({
multiple: true,
maxCount: 3,
copyToCache: true,
fileTypes: ['image', 'doc'],
extensions: ['jpg', 'png', 'pdf'],
success: (res) => {
const files: LaoFileRecord[] = res.files
console.log('picked files', files)
},
fail: (err) => {
console.log('pick failed', err.errCode, err.errMsg)
}
})
3. 创建工作区并写入文本
import {
createEntry,
resolveDirectory,
writeTextContent
} from '@/uni_modules/laoqianjunzi-file'
resolveDirectory('sandbox-files').then((dirRes) => {
if (!dirRes.success || dirRes.path == null) {
return
}
const workspace = dirRes.path + '/laoqianjunzi-file-demo'
createEntry(workspace, false).then((folderRes) => {
if (!folderRes.success || folderRes.path == null) {
return
}
const textPath = folderRes.path + '/note.txt'
writeTextContent(textPath, 'hello uni-app x', false).then((writeRes) => {
console.log('write result', writeRes)
})
})
})
4. 读取目录与文本
import {
readDirectory,
readTextContent
} from '@/uni_modules/laoqianjunzi-file'
readDirectory('/your/workspace', 'txt').then((listRes) => {
console.log('txt files', listRes.files)
})
readTextContent('/your/workspace/note.txt').then((textRes) => {
console.log('content', textRes.content)
})
5. 上传本地文件
import { uploadLocalFile } from '@/uni_modules/laoqianjunzi-file'
uploadLocalFile({
url: 'https://httpbin.org/post',
filePath: '/your/local/file.txt',
name: 'file',
formData: {
scene: 'demo'
} as UTSJSONObject,
Update: (progress) => {
console.log('upload progress', progress.progress)
},
success: (res) => {
console.log('upload success', res.statusCode, res.data)
},
fail: (err) => {
console.log('upload fail', err.errCode, err.errMsg)
}
})
6. 批量下载
import {
downloadRemoteFiles,
resolveDirectory
} from '@/uni_modules/laoqianjunzi-file'
resolveDirectory('sandbox-cache').then((dirRes) => {
if (!dirRes.success || dirRes.path == null) {
return
}
downloadRemoteFiles(dirRes.path, [
'https://www.w3.org/TR/PNG/iso_8859-1.txt'
]).then((downloadRes) => {
console.log('download result', downloadRes.savedPaths)
})
})
API 说明
类型定义
FilePickerGroup
可选值:
allimagevideoaudiodoctextarchivefile
FileSystemBucket
可选值:
external-cacheexternal-filessandbox-cachesandbox-filespublic-root
EntryKind
可选值:
anyfiledirectory
LaoFileRecord
| 字段 | 类型 | 说明 |
|---|---|---|
name |
string |
文件名 |
uri |
string \| null |
平台定位 URI |
path |
string \| null |
本地可访问路径 |
size |
number \| null |
文件大小,单位字节 |
mimeType |
string \| null |
MIME 类型 |
extension |
string |
后缀名,不带点 |
duration |
number |
音视频时长,非音视频场景通常为 0 |
isDirectory |
boolean |
是否为目录 |
modifiedTime |
number \| null |
最后修改时间 |
文件选择
selectLocalFiles(options)
参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
multiple |
boolean |
否 | 是否允许多选 |
maxCount |
number |
否 | 最大选择数量,大于 1 时会自动启用多选 |
copyToCache |
boolean |
否 | 是否将选择结果复制到插件可访问缓存区,默认推荐保持 true |
filename |
string |
否 | 为复制后的结果指定文件名;多文件场景会自动追加序号 |
fileTypes |
FilePickerGroup[] |
否 | 选择器分组过滤 |
extensions |
string[] |
否 | 后缀过滤,传入后会优先于分组推导 |
mimeTypes |
string[] |
否 | MIME 过滤,用于进一步细化可选范围 |
success |
(res) => void |
否 | 选择成功回调 |
fail |
(err) => void |
否 | 选择失败回调 |
complete |
(res) => void |
否 | 完成回调 |
成功结果
success 回调中最重要的结果字段是:
files:统一后的LaoFileRecord[]
实际业务中建议只以 files 为准继续后续上传、复制、读写等流程。
上传
uploadLocalFile(options)
参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
url |
string |
是 | 远程上传地址 |
filePath |
string |
是 | 本地文件路径 |
name |
string |
是 | 表单字段名 |
header |
UTSJSONObject \| null |
否 | 自定义请求头 |
formData |
UTSJSONObject \| null |
否 | 额外表单参数 |
timeout |
number |
否 | 超时时间,单位毫秒 |
success |
(res) => void |
否 | 上传成功回调 |
fail |
(err) => void |
否 | 上传失败回调 |
complete |
(res) => void |
否 | 完成回调 |
onProgressUpdate |
(res) => void |
否 | 上传进度回调 |
成功结果
| 字段 | 类型 | 说明 |
|---|---|---|
errMsg |
string |
固定为 uploadLocalFile:ok |
data |
string |
服务端响应文本 |
statusCode |
number |
HTTP 状态码 |
权限与能力
requestFileAccess()
返回 Promise<PermissionResult>。
| 字段 | 类型 | 说明 |
|---|---|---|
success |
boolean |
本次权限流程是否成功 |
code |
number |
结果码 |
message |
string |
说明文本 |
granted |
boolean |
是否已获得所需权限 |
grantedPermissions |
string[] |
当前已授予权限列表 |
说明:
- Android 会执行真实运行时权限申请
- iOS 与 Harmony 以当前沙箱访问能力为主
- Web 直接返回可用状态,不涉及原生存储授权
describeCapabilities()
同步返回 CapabilityResult,用于判断当前平台应展示哪些入口。
目录与文件系统
resolveDirectory(bucket)
返回 Promise<PathResult>,用于拿到当前平台的基础目录。
各目录桶说明
| 目录桶 | Android | iOS | Harmony | Web |
|---|---|---|---|---|
sandbox-cache |
应用缓存目录 | 应用缓存目录 | 应用缓存目录 | 不支持 |
sandbox-files |
应用文件目录 | 应用文档目录 | 应用文件目录 | 不支持 |
external-cache |
外部缓存目录 | 映射到沙箱下的 external-cache 子目录 |
映射到缓存目录 | 不支持 |
external-files |
外部文件目录 | 映射到沙箱下的 external-files 子目录 |
映射到文件目录 | 不支持 |
public-root |
外部公开存储根目录 | 不支持 | 不支持 | 不支持 |
createEntry(targetPath, createFile?)
createFile = false:创建目录createFile = true:创建文件
返回 Promise<PathResult>。
copyEntry(sourcePath, targetPath, overwrite?)
复制文件或目录,返回 Promise<PathResult>。
moveEntry(sourcePath, targetPath, overwrite?)
移动文件或目录,返回 Promise<PathResult>。
deleteEntry(targetPath)
删除文件或目录,返回 Promise<OperationResult>。
existsEntry(targetPath, kind?)
检查目标是否存在,返回 Promise<ExistsResult>。
measureEntry(targetPath)
读取文件或目录大小,返回 Promise<SizeResult>。
readDirectory(targetPath, suffix?)
读取目录第一层子项,返回 Promise<FileListResult>。
说明:
suffix可传txt、jpg、zip等后缀- 传空字符串表示不过滤
- 目录项会一并返回,便于做文件浏览与继续下钻
文本读写
readTextContent(targetPath)
返回 Promise<TextResult>。
补充说明:
- App 端读取本地文本文件
- Web 端读取可访问的文本 URL 或同源资源
writeTextContent(targetPath, content, append?)
返回 Promise<PathResult>。
append = false:覆盖写入append = true:追加写入
下载、压缩与解压
downloadRemoteFiles(saveDirectory, urls)
批量下载远程文件,返回 Promise<DownloadResult>。
结果结构:
| 字段 | 类型 | 说明 |
|---|---|---|
success |
boolean |
是否成功 |
code |
number |
结果码 |
message |
string |
结果说明 |
savedPaths |
string[] |
已保存的文件路径 |
compressDirectory(directoryPath, archivePath, password?)
压缩目录,返回 Promise<PathResult>。
compressEntries(entryPaths, archivePath, password?)
压缩指定文件或目录集合,返回 Promise<PathResult>。
extractArchive(archivePath, targetDirectory, password?)
解压归档文件,返回 Promise<PathResult>。
说明:
- Android 支持压缩、解压、密码参数
- iOS、Harmony、Web 当前会返回
9801020
状态控制
updateReadOnlyState(targetPath, readOnly?, recursive?)
设置只读状态,返回 Promise<OperationResult>。
说明:
- Android 支持
- iOS、Harmony、Web 当前不支持
updateHiddenState(targetPath, hidden?)
设置隐藏状态,返回 Promise<OperationResult>。
说明:
- Android 支持
- iOS、Harmony、Web 当前不支持
错误码
| 错误码 | 含义 |
|---|---|
9801001 |
用户取消操作 |
9801002 |
当前平台不支持此能力 |
9801003 |
运行时上下文不可用 |
9801004 |
权限被拒绝 |
9801005 |
参数不合法 |
9801006 |
文件类型不匹配 |
9801007 |
源路径不存在 |
9801008 |
目标路径已存在 |
9801009 |
读取文件失败 |
9801010 |
写入文件失败 |
9801011 |
复制文件失败 |
9801012 |
移动文件失败 |
9801013 |
删除文件失败 |
9801014 |
读取目录失败 |
9801015 |
压缩归档失败 |
9801016 |
解压归档失败 |
9801017 |
上传文件失败 |
9801018 |
下载文件失败 |
9801019 |
路径解析失败 |
9801020 |
当前平台暂不支持该文件能力 |
实战建议
- 业务代码请优先调用
describeCapabilities()决定 UI 展示与按钮可用性 - 文件选择后请直接基于
files字段继续流程,不要依赖兼容字段 - 需要跨端一致行为时,优先使用
sandbox-cache与sandbox-files - Web 端更适合作为选择、上传、远程文本读取的轻量入口,不建议假设其拥有本地文件系统能力
- 批量下载、压缩、解压建议先落到工作区目录,再由业务决定是否继续复制或上传
示例页联调建议
若你想快速验证插件能力,建议直接打开:
uni_modules/laoqianjunzi-file/pages/index.uvue
建议验证顺序:
- 先点“读取能力”和“检查权限”确认平台声明
- 再执行“准备工作区”“写入文本”“读取目录”验证基础文件系统
- 最后执行“上传”“下载”“压缩”“解压”验证进阶链路

收藏人数:
https://gitee.com/laoqianjunzi/laoqianjunzi-file
下载插件并导入HBuilderX
下载示例项目ZIP
赞赏(0)
下载 1158
赞赏 2
下载 12348238
赞赏 1925
赞赏
京公网安备:11010802035340号