微信小程序API之wx.uploadFile上传文件踩坑
微信小程序API之wx.uploadFile上传文件踩坑:wx.uploadFile 返回的是 string 字符串格式,需要字符串对象化。
wx.chooseImage({
success (res) {
const tempFilePaths = res.tempFilePaths
wx.uploadFile({
url: 'https://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址
filePath: tempFilePaths[0],
name: 'file',
formData: {
'user': 'test'
},
success (res){
//踩坑:wx.uploadFile 返回的是 string 字符串格式,需要字符串对象化
var data = JSON.parse(res.data);
//do something
//未开启图片上传功能
if (data.status == 501) {
wx.showToast({
title: res.message,
icon: 'none'
})
}
else{
wx.showToast({
title: '上传成功',
});
}
}
})
}
})