uniapp文件缓存技术(非tree版本)

4 篇文章 0 订阅

话不多说上代码

用的封装方式

只需要导入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
			}
		})
	}

}

 使用的时候传个 地址 文件名 和文件类型即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

栈狮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值