话不多说,上解决方案
wx.getSavedFileList({ // 获取文件列表
success(res) {
res.fileList.forEach((val, key) => { // 遍历文件列表里的数据
// 删除存储的垃圾数据
wx.removeSavedFile({
filePath: val.filePath
});
})
setTimeout(function(){
wx.saveFile({
tempFilePath: ptah,
success: function success(resp) {
wx.hideLoading()
wx.showToast({
title: '长按图片保存到手机',
icon: 'none',
duration: 2000
})
let preUrl = resp.savedFilePath
wx.previewImage({
current: preUrl, // 当前显示图片的http链接
urls: [preUrl] // 需要预览的图片http链接列表
})
},
fail: function (resp) {
}
});
},1500)
}
})
问题描述:
最近小程序上做了一个生成海报的功能,开始功能一直没问题,上线以后也能生成,但是用了几次以后就没法生成了,逐行排除代码,原来是小程序的本地存储满了。
报错信息:
saveFile:fail the maximum size of the file storage limit is exceeded
报错原因:
微信文档中描述的是 本地文件存储的大小限制为 10M
解决方案
wx.getSavedFileList,获取小程序本地存储的文件列表,再通过wx.removeSavedFile将垃圾文件删除即可。我的代码加了setTimeout保证执行顺序。