更新记录
1.0.5(2025-07-15)
新的调用修改
1.0.4(2024-09-28)
修复 选择文件 时候重复多次选择后,会提示被移除的监听
1.0.3(2024-04-28)
修复页面销毁后再获取赋值后页面数据不更新的bug
查看更多平台兼容性
云端兼容性
阿里云 | 腾讯云 | 支付宝云 |
---|---|---|
√ | √ | √ |
uni-app(4.75)
Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
---|---|---|---|---|---|---|---|---|
√ | √ | × | × | √ | - | 5.0 | × | - |
微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
---|---|---|---|---|---|---|---|---|---|---|
× | × | × | × | × | × | × | × | × | × | × |
uni-app x(4.75)
Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
---|---|---|---|---|---|
- | - | - | - | - | - |
原生安卓Android文件选择器 返回一个临时文件地址 下载文件后保存到手机
vue2需要自己去修改下写法,以下是vue3的案例
需要先打包为自定义调试包,才可以调试
调用插件 :
import {
UTSchoosefile,
UTScopyfile,
UTSgetStorage
} from "@/uni_modules/mushan-choosefile";
完整页面调用案例
<template>
<view>
<button @click="onUTSchoosefile">打开选择文件</button>
<button @click="onUTScopyfile">获取文件后保存到手机</button>
<button @click="onUTSgetStorage">获取手机的内部存储地址和外部存储地址</button>
</view>
</template>
<script setup>
import {
onShow,
onReady,
onReachBottom,
onPullDownRefresh,
onShareAppMessage
} from '@dcloudio/uni-app';
import {
getCurrentInstance,
ref,
reactive,
inject,
computed,
onMounted
} from 'vue';
import {
UTSchoosefile,
UTScopyfile,
UTSgetStorage,
} from "@/uni_modules/mushan-choosefile";
//选择器选择后,返回一个临时文件名称、大小,地址
function onUTSchoosefile(){
UTSchoosefile((res)=>{
let data = JSON.parse(res)
console.log(data)
console.log(data.name)
console.log(data.size)
console.log(data.file)
//可通过uni.uploadFile 把文件fileData 传给后端服务器
let fileData = `file://${data.file}`;
})
}
//获取手机对应的外部和内部存储地址路径
function onUTSgetStorage(){
UTSgetStorage((res)=>{
let data = JSON.parse(res);
console.log(data);
})
}
//例子 通过文件链接地址下载文件后,保存到手机
/*
UTScopyfile(cleanPath, fileName, (res) => {
let data = JSON.parse(res);
console.log(data);
});
UTScopyfile 需要传的参数 cleanPath 为文件地址路径,fileName为文件名称
*/
function onUTScopyfile(){
//文件的下载地址
let fileUrl = 'http://xxxx.xxx.xxxx';
uni.downloadFile({
url: fileUrl,
success: (res) => {
if (res.statusCode === 200) {
console.log(res);
console.log(res.tempFilePath);
plus.io.resolveLocalFileSystemURL(res.tempFilePath,(entry)=>{
//文件名称
let fileName = entry.name;
//文件地址
let fullPath = entry.toLocalURL();
let cleanPath = fullPath.replace('file://', '');
UTScopyfile(cleanPath, fileName, (res) => {
let data = JSON.parse(res);
console.log(data);
});
},(e)=>{
console.error('解析失败:', e.message);
});
}else{
uni.showToast({
title: '下载失败',
duration: 2000,
icon: 'none'
});
}
},
fail:()=>{
uni.showToast({
title: '下载失败',
duration: 2000,
icon: 'none'
});
}
});
}
</script>
<style>
</style>