微信小程序中 在用戶信息中關於用戶頭像更換(拍照或相冊上傳)功能實現。
圖像點擊觸發事件:
<image src='{{personImage}}' bindtap='changeAvatar' ></image>
Page({
data:{},
changeAvatar:function(){
const _this = this;
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success(res) {
// tempFilePath可以作為img標簽的src屬性顯示圖片
const tempFilePath = res.tempFilePaths[0];
_this.setData({
personImage: tempFilePath
})
wx.uploadFile({
url: config.UPLOADFILE, //圖片上傳至開發服務器接口
filePath: tempFilePath,
name: 'file',
formData: {},
success(res) {
const data = res.data;
console.log(data);
}
})
}
})
}
})
