话不多说上代码
用的封装方式
只需要导入19行的
使用只需要38行的
这个代码存到你封装的对应位置
export const getFileCache = function(filePath, fileName, fileType) {
// 缓存key值
let storageKey = 'FILE_CACHE_INFO_' + fileName + fileType
const cacheFileInfo = uni.getStorageSync(storageKey)
if (cacheFileInfo) {
console.log("已缓存为:" + cacheFileInfo)
return cacheFileInfo
} else {
console.log("未缓存,进行下载保存")
// 如果没有,执行下载,并存储起来后
uni.downloadFile({
url: filePath,
success: (res) => {
if (res.statusCode === 200) {
console.log('下载成功');
// 再进行本地保存
uni.saveFile({
tempFilePath: res.tempFilePath,
success: function(res2) {
console.log(res2.savedFilePath)
uni.setStorageSync(storageKey, res2.savedFilePath)
return res2.savedFilePath
},
fail: function(res2) {
return filePath
}
})
} else {
console.log('下载临时文件失败')
return filePath
}
},
fail: (res) => {
console.log(res)
return filePath
}
})
}
}
使用的时候传个 地址 文件名 和文件类型即可