更新记录
1.0.0(2025-06-10)
新增功能
- 新增了
initFilePermission
,getAppPath
,getPrivatePath
,getPublicPath
,zipFolder
,zipFiles
,zipDecompress
,createPath
,copyPath
,movePath
,deletePath
,setReadOnly
,setHide
,checkPath
,pathSize
,readFile
,writeFile
,downloadFile
等方法 setReadOnly
方法还存在些许问题,正修复中(若修复不了,后期可能删除)- 初始版本
问题修复
- 无
功能优化
- 无
平台兼容性
uni-app
Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
---|---|---|---|---|---|---|---|---|
√ | √ | - | - | - | - | 5.0 | × | × |
微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
---|---|---|---|---|---|---|---|---|---|---|
× | × | × | × | × | × | - | × | × | × | × |
uni-app x
Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
---|---|---|---|---|---|
- | - | 5.0 | × | × | × |
文件操作插件,支持文件读写、文件下载、文件复制、文件压缩、文件解压等
ima-files
是一款文件操作插件的UTS插件,支持给操作权限
、私有存储空间
、内部存储
、公有存储空间
、
压缩文件夹
、压缩文件
、解压zip
、创建目录(支持文件或目录)
、复制文件或目录
、移动文件或目录
、
删除文件或目录
、设置只读
、设置隐藏
、判断文件或目录是否存在
、获取文件|目录大小
、读取文件内容
、
写入文件内容
、批量下载文件
等功能,主要应用于在手机上的一些文件操作等
⚠️注意️
android 端
已全部完成代码的开发与测试(已完成)【除设置只读(setReadOnly)
外】harmony 端
(计划中)
需要权限
- 文件读取、文件写入
"android.permission.INTERNET"
"android.permission.READ_EXTERNAL_STORAGE"
"android.permission.WRITE_EXTERNAL_STORAGE"
- 即:在
manifest.json
中的distribute.android.permissions
加入
// 联网(下载文件需要)
<uses-permission android:name="android.permission.INTERNET" />
// 文件读取
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
// 文件写入
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
使用示例【此示例只实现了压缩文件夹
、批量下载文件
逻辑,更多示例请导入项目】
- 新建一个
test.vue
的文件 - ⚠️注意️:若导入示例,请先在手机目录下建‘Test/data/’的目录,
- 1、在目录下创建
123456789.png
、123456789.txt
的文件 - 2、在目录下创建
data2.zip
、data1.zip
的压缩文件 - 3、在目录下创建
aaa
、bbb
、ccc
的目录文件夹
<template>
<view class="content">
<!-- 压缩与解压 -->
<view class="content-title">压缩与解压 📦</view>
<view class="content-message">(可压缩、解压文件夹|多文件,支持加密)</view>
<view class="content-button ima-xxx" @click="onZipFolder">压缩文件夹(可加密)</view>
<!-- 文件操作 -->
<view class="content-title">文件操作 📁</view>
<view class="content-message">(支持私有、公有、内部存储空间的相互操作)</view>
<view class="content-button ima-xxx" @click="onDownloadFile">批量下载文件</view>
</view>
</template>
<script>
import {initFilePermission, zipFolder, downloadFile}
}
from
'@/uni_modules/ima-files'
let _this = null
export default {
data() {
return {}
},
onLoad() {
initFilePermission().then((data: IReturn) => {
console.log('给操作权限结果:', data)
})
},
methods: {
// 压缩文件夹
onZipFolder() {
zipFolder(
'/storage/emulated/0/Test/data',
'/storage/emulated/0/IMA/data/zip-file.zip',
'123321'
).then((data: IReturn) => {
console.log('压缩文件夹', data)
})
},
// 压缩文件夹[async\await]
async onZipFolder2() {
const data: IReturn = await zipFolder(
'/storage/emulated/0/Test/data',
'/storage/emulated/0/IMA/data/zip-file.zip',
'123321'
);
console.log('压缩文件夹', data)
},
// 批量下载文件
onDownloadFile() {
downloadFile('/storage/emulated/0/Test/Download', [
'http://gips0.baidu.com/it/u=3602773692,1512483864&fm=3028&app=3028&f=JPEG&fmt=auto?w=960&h=1280',
'http://gips0.baidu.com/it/u=3560029307,576412274&fm=3028&app=3028&f=JPEG&fmt=auto?w=960&h=1280'
]).then((data: IReturn) => {
console.log('批量下载文件', data)
})
}
}
}
</script>
<style lang="scss">
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
&-title {
margin-top: 10rpx;
padding: 10rpx;
font-size: 44rpx;
font-weight: 800;
color: #ff0000;
}
&-message {
font-size: 28rpx;
font-weight: 800;
text-align: center;
color: #ff6700;
}
&-button {
height: 65rpx;
width: 750rpx;
padding: 20rpx;
line-height: 65rpx;
text-align: center;
background: #00e0f1;
margin-bottom: 25rpx;
}
}
</style>
方法
方法名称 | 说明 | 方法参数 | 平台 |
---|---|---|---|
initFilePermission | 给操作权限 | () => Promise |
android |
getAppPath | 私有存储空间 | (type ?: rootPathType) => Promise |
android |
getPrivatePath | 内部存储 | (type ?: rootPathType) => Promise |
android |
getPublicPath | 公有存储空间 | () => Promise |
android |
zipFolder | 压缩文件夹 | (folderPath : String, zipPath : String, password : String) => Promise |
android |
zipFiles | 压缩文件 | (filePaths : Array |
android |
zipDecompress | 解压zip | (zipPath : String, destDir : String, password : String) => Promise |
android |
createPath | 创建目录(支持文件或目录) | (path : string, isFile ?: boolean) => Promise |
android |
copyPath | 复制文件或目录 | (tsrcPath : string, destPath : string, overwrite ?: boolean) => Promise |
android |
movePath | 移动文件或目录 | (srcPath : string, destPath : string, overwrite ?: boolean) => Promise |
android |
deletePath | 删除文件或目录 | (path : string) => Promise |
android |
setReadOnly | 设置只读 | (path : string, isReadOnly ?: boolean) => Promise |
android |
setHide | 设置隐藏 | (path : string, isHide ?: boolean) => Promise |
android |
checkPath | 判断文件或目录是否存在 | (path : string, type ?: pathType) => Promise |
android |
pathSize | 获取文件|目录大小 | (path : string) => Promise |
android |
readFile | 读取文件内容 | (path : string) => Promise |
android |
writeFile | 写入文件内容 | (path : string, content : string, append ?: boolean) => Promise |
android |
downloadFile | 批量下载文件 | (saveDir : String, urls : Array |
android |