微信小程序头像上传功能,解决图片base64上传转码问题
<button open-type="chooseAvatar" @chooseavatar="onChooseAvatar"> <image class="avatar" :src="member.avatar+Math.floor(Math.random() * 10000)"></image> </button>
onChooseAvatar(e) {
let that = this;
const {avatarUrl} = e.detail;
wx.getFileSystemManager().readFile({
filePath: avatarUrl, //要读取的文件的路径 (本地路径)
encoding: "base64", //指定读取文件的字符编码
success(res) {
//转换完毕,执行上传
let avatar=encodeURI('data:image/png;base64,' + res.data).replace(/\+/g,'%2B');//避免特殊字符+ 传输变成空格,重要
that.http.post('&s=member&c=account&m=avatar&r=9351','is_ajax=1&file='+avatar) //我这里是单独封装过的请求方式,按照官方头像上传接口传输内容即可https://www.xunruicms.com/doc/1081.html
.then(obj => {
uni.setStorageSync('member', obj.data.data); //按照自己的用户保存方式,更新用户信息
that.member= obj.data.data;
uni.showToast({
icon:false,
title: obj.data.msg,
duration: 2000
});
}).catch(err => {
console.log(err)
})
}
})
},