uniapp下载&打开实现方案,支持安卓ios和h5
Android:
1、申请本地存储读写权限
2、创建文件夹(文件夹不存在即创建)
3、下载文件
ios:
1、下载文件
2、保存到本地,需要打开文件点击储存
使用方法:
js
downloadFile(fileUrl, fileName)
file.js
js
let downloadFilePath = '/storage/emulated/0/yulong-ys-files'
// 创建文件夹
export const createDir = () => {
return new Promise((resolve, reject) => {
// 申请本地存储读写权限
plus.android.requestPermissions([
'android.permission.WRITE_EXTERNAL_STORAGE',
'android.permission.READ_EXTERNAL_STORAGE',
'android.permission.INTERNET',
'android.permission.ACCESS_WIFI_STATE'
], success => {
const File = plus.android.importClass('java.io.File')
let file = new File(downloadFilePath)
// 文件夹不存在即创建
if (!file.exists()) {
file.mkdirs()
resolve()
}
resolve()
}, error => {
Tips.toast('无法获取权限,文件下载将出错')
reject(error)
})
})
}
// 下载文件操作
async function doDownloadFile(url, fileName, options, osName) {
if (osName === 'Android') {
await createDir()
}
Tips.loading('正在下载...')
let dTask = plus.downloader.createDownload(url, options, async (d, status) => {
Tips.hideLoading()
if (status == 200) {
plus.io.convertLocalFileSystemURL(d.filename)
await Tips.confirm('文件已保存,是否打开?')
uni.openDocument({
filePath: d.filename,
success: () => {
console.log('成功打开')
}
})
} else {
console.log('下载失败')
console.log(d)
Tips.toast('下载失败')
Tips.hideLoading()
plus.downloader.clear()
}
})
dTask.start()
}
// 下载文件
export function downloadFile(url, fileName) {
if (!url) {
Tips.toast('下载地址不能为空')
return Promise.reject('下载地址不能为空')
}
// #ifdef H5
window.location.href = url
// #endif
// #ifdef APP-PLUS
let osName = plus.os.name;
if (osName === 'Android') {
doDownloadFile(url, fileName, {filename: 'file://' + downloadFilePath + '/' + fileName}, osName)
} else {
doDownloadFile(url, fileName, {}, osName)
}
// #endif
}
Tips.js
js
/**
* 提示与加载工具类
*/
export default class Tips {
constructor() {
this.isLoading = false;
}
/**
* 弹出提示框
*/
static success(title, duration = 1000) {
setTimeout(() => {
uni.showToast({
title: title,
icon: "success",
mask: true,
duration: duration
});
}, 300);
if (duration > 0) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, duration);
});
}
}
/**
* 弹出确认窗口
*/
static confirm(content, ops = {}, payload = {}) {
return new Promise((resolve, reject) => {
uni.$showModal({
content: content,
...ops,
success: res => {
if (res.confirm) {
resolve(payload);
} else if (res.cancel) {
reject(payload);
}
},
fail: res => {
reject(payload);
}
});
});
}
static toast(title, onHide = undefined, icon = "none") {
setTimeout(() => {
uni.showToast({
title: title,
icon: icon,
mask: true,
duration:1500
});
}, 0);
// 隐藏结束回调
if (onHide) {
setTimeout(() => {
onHide();
}, 1500);
}
}
/**
* 错误框
*/
static error(title, onHide) {
uni.showToast({
title: title,
icon: 'error',
mask: true,
duration: 1500
});
// 隐藏结束回调
if (onHide) {
setTimeout(() => {
onHide();
}, 1500);
}
}
/**
* 弹出加载提示
*/
static loading(title = "加载中") {
if (Tips.isLoading) {
return;
}
Tips.isLoading = true;
uni.showLoading({
title: title,
mask: true
});
}
/**
* 加载完毕
*/
static hideLoading() {
if (Tips.isLoading) {
Tips.isLoading = false;
uni.hideLoading();
}
}
}
/**
* 静态变量,是否加载中
*/
Tips.isLoading = false;