posts - 23,comments - 2,views - 25824

小程序项目中遇到需要更换用户头像的需求,这里记录下,相关逻辑代码index.ts,只选取重要的如下:

// 更换用户头像
changeAvatar() {
const that = this
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success(res) {
wx.showLoading({
title: "上传中...",
mask: true,
});
// tempFilePath可以作为img标签的src属性显示图片
const tempFilePath = res.tempFilePaths[0]
console.log(tempFilePath, res);
wx.uploadFile({
url: 'http://192.168.188.226:22000/ech-sad/v1/upload/img', // 上传图片的服务器地址,写自己项目中的即可
filePath: tempFilePath, // 上传图片的本地路径
name: 'imagefile',
header: {
Authorization: wx.getStorageSync("Authorization"),
}, // 设置请求头,携带上token
formData: {
imagefile: tempFilePath,
'type': '3',
}, // 后端需要的请求体参数
success(resp) {
wx.hideLoading();
// 返回的是json字符串类型数据,要转换为json对象
const json_data = JSON.parse(resp.data)
const image = json_data.data
console.log('aaa', image);
that.setData({
buttonDisabled: false,
image
})
}
})
}
})
},
posted on   九零菜菜  阅读(785)  评论(1编辑  收藏  举报
编辑推荐:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示