一、需求
ios和Android,下载文件到指定目录
二、代码
Android:
1、申请本地存储读写权限
2、创建文件夹(文件夹不存在即创建)
3、下载文件
ios:
1、下载文件
2、保存到本地,需要打开文件点击储存
// 创建文件夹,path值为:"/storage/emulated/0/自定义文件夹名称"
export const createDir = async (path, callback) => {
// 申请本地存储读写权限
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(path)
// 文件夹不存在即创建
if (!file.exists()) {
file.mkdirs()
callback && callback()
return false
}
callback && callback()
return false
}, error => {
uni.$u.toast('无法获取权限,文件下载将出错')
})
}
// 下载文件
export const downLoadFile = (file) => {
// #ifdef APP-PLUS
let osName = plus.os.name
if (osName === 'Android') {
let mkdirsName = '/HNZY'
// const Environment = plus.android.importClass('android.os.Environment')
// Environment.getExternalStorageDirectory() === '/storage/emulated/0'
let path = '/storage/emulated/0' + mkdirsName
// 创建文件夹MyDownload
createDir(path, () => {
uni.showLoading({
title: '正在下载'
})
let dtask = plus.downloader.createDownload(getFullUrl(file.fileUrl), {
filename: 'file://' + path + '/' + file.originalName
}, function(d, status) {
uni.hideLoading()
if (status == 200) {
let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename)
// console.log('d.filename', d.filename)
// console.log('fileSaveUrl', fileSaveUrl)
// plus.runtime.openFile(d.filename)
uni.showToast({
icon: 'none',
mask: true,
//保存路径
title: '文件已保存:' + mkdirsName + '/' + file.originalName,
duration: 3000
})
} else {
plus.downloader.clear()
}
})
dtask.start()
})
} else {
uni.showLoading({
title: '正在下载'
})
let dtask = plus.downloader.createDownload(getFullUrl(file.fileUrl), {
// filename: '/Library/Pandora/downloads/' + file.originalName
}, function(d, status) {
uni.hideLoading()
if (status == 200) {
let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename)
// console.log('d.filename', d.filename)
// console.log('fileSaveUrl', fileSaveUrl)
// plus.runtime.openFile(d.filename)
uni.showModal({
title: '提示',
content: '如需保存到本地,需要打开文件点击储存',
// cancelText: '我知道了',
confirmText: '打开文件',
success: function(res) {
if (res.confirm) {
uni.openDocument({
filePath: d.filename,
success: (sus) => {
// console.log('成功打开')
}
})
}
}
})
} else {
plus.downloader.clear()
}
})
dtask.start()
}
// #endif
// #ifdef H5
window.open(getFullUrl(file.fileUrl))
// #endif
// #ifdef MP
uni.setClipboardData({
data: getFullUrl(fileUrl),
success: () => {
uni.$u.toast('链接已复制,请在浏览器打开')
}
})
// #endif
}
// 工具类
export function getFullUrl(url) {
let fullUrl = ''
if (url) {
fullUrl = /(http|https|blob:):\/\/([\w.]+\/?)\S*/.test(url) ?
url :
config.filePath + url
}
return fullUrl
}
export const config = {
baseUrl: "/api",
filePath: "http://113.22.33.44:280",
// #ifdef APP-PLUS
// 开发环境
baseUrl: "http://113.22.33.44:280/stage-api",
filePath: "http://113.22.33.44:280",
// #endif
}
// 使用
let file = {
id": "1867120817079283712",
fileSuffix: ".pdf",
originalName: "test.pdf",
fileSize: 20795,
createTime: "2024-12-12 16:14:26",
createUser: "155",
fileUrl: "/stage-api/so-fast-fs/obsFileInfo/down/1867120817079283712",
filePreviewUrl: "/obsPreview/test.pdf",
createUserName: "檀健次"
}
downLoadFile(file)