wx.chooseImage({
count: 1, // 張數, 默認9
sizeType: ['compressed'], // 建議壓縮圖
sourceType: ['album'], // 來源是相冊 camera:拍照
success: (res) => {
if (res.tempFiles[0] && res.tempFiles[0].size > 5242880) {
wx.showToast({
title: '請上傳小於5M的圖片',
icon: 'none',
});
return false;
}
this.uploadImage(res.tempFilePaths[0]);
},
});
uploadImage(tempavatarUrl) {
wx.uploadFile({
url: url,//鏈接地址
filePath: tempavatarUrl,
name: 'file',
header: {
'Content-Type': 'multipart/form-data',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
},
success: (res) => {
const data = JSON.parse(res.data);
if (data.success===true) {
console.info('success')
} else {
wx.showToast({
title: '上傳頭像失敗',
icon: 'none',
});
}
},
});
},