微信小程序頭像上傳功能,解決圖片base64上傳轉(zhuǎn)碼問題
<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) {
//轉(zhuǎn)換完畢,執(zhí)行上傳
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) //我這里是單獨(dú)封裝過的請(qǐng)求方式,按照官方頭像上傳接口傳輸內(nèi)容即可http://www.apdwn.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)
})
}
})
},