uniapp小程序实现更换头像

wx.chooseMedia

功能描述

拍摄或从手机相册中选择图片或视频

温馨提示:不建议使用:wx.chooseImage,原因:从基础库 2.21.0 开始,本接口停止维护,请使用 wx.chooseMedia 代替

 

 

 

uni.chooseMedia({
count: 1,
sourceType: ['album'],
success: res => {
uni.getImageInfo({
src: res.tempFiles[0].tempFilePath,
success: image => {
console.log(image)
uni.showLoading({ title: '图片上传中', mask: true })
uni.uploadFile({
url: `${VUE_APP_API_URL}/api/upload`,
file: image,
filePath: image.path,
header: {
Authorization: 'Bearer ' + store.getters.token,
},
name: 'file',
success: res => {
if (callback) {
callback(JSON.parse(res.data).link)
}
},
fail: err => {
uni.showToast({
title: '上传图片失败',
icon: 'none',
duration: 2000,
})
},
complete: res => {
uni.hideLoading()
},
})
},
fail: err => {
uni.showToast({
title: '获取图片信息失败',
icon: 'none',
duration: 2000,
})
},
})
},
})

posted @ 2022-11-23 11:29  danmo_xx  阅读(1299)  评论(0编辑  收藏  举报