今日写小程序上传图片时遇到了点困难,图片上传到接口保存到数据库,用tp5直接接收name传参进行正常的上传文件操作就行,但是如何让小程序上传的图片在刷新页面之后还能继续存在呢,这问题着实让我苦恼了一阵子,最后终于在朋友的提醒下成功了,哈哈哈哈
解决办法:
用小程序的数据缓存接口 wx.setStorageSync(KEY,DATA) 存到本地缓存,然后页面加载的时候用 wx.getStorageSync(KEY) 取出来就可以啦
下面上代码
wx.chooseImage({ count:
1, sizeType: [
'original',
'compressed'], sourceType: [
'album',
'camera'], success:
function(res) { var filepath = res.tempFilePaths wx.uploadFile({ url: getApp().data.url +
'member/imgVipbaseInfo?openid='+wx.getStorageSync(
'openid'), filePath: filepath[
0], name:
'photo', success:
function(res) { wx.downloadFile({ url: filepath[
0], success:
function(res){ var tempFilePaths = res.tempFilePath wx.saveFile({ tempFilePath: tempFilePaths, success:
function (res) { var savedFilePath = res.savedFilePath wx.setStorageSync(
'img', savedFilePath); that.setData({ photo: wx.getStorageSync(
'img') }) } }) } })
if (res.statusCode==
200){ wx.showToast({ title:
'上传成功', icon:
'success', duration:
1500 }) }
else{ wx.showToast({ title:
'上传失败', icon:
'none', duration:
1500 }) } }, fail:
function(res) {}, complete:
function(res) {}, }) }, fail:
function(res) {}, complete:
function(res) {}, })
小程序
TAG标签:
微信小程序图片缓存清理
http://www.xiaochengxub.com/read-106551.html