uploadImage: function () {
var that = this
wx.chooseImage({
success: function (res) {
var tempFilePaths = res.tempFilePaths
wx.showToast({
title: '识别中,请稍候',
icon: 'loading',
duration: 2000
})
console.log(tempFilePaths[0])
wx.uploadFile({
url: 'https://www.hanqi86.wang/uploads/WX/',
header: {
'content-type': 'multipart/form-data'
},
filePath: tempFilePaths[0],
name: 'file',
formData: {
'user': 'test'
},
success: function (res) {
console.log("上传成功");
console.log(res.data)
wx.hideToast()
var data = JSON.parse(res.data)
if (!data.attributes) {
that.setData({
userInfo: {
avatarUrl: data.url,
tips: '未识别到颗粒'
}
})
return
}
const genders = {
'Male': '帅哥',
'Female': '美女'
}
that.setData({
userInfo: {
avatarUrl: data.url,
tips: '一位' + data.attributes.age.value + '岁的' + genders[data.attributes.gender.value]
}
})
//do something
},
fail: function (res) {
console.log("上传失败" + res.data);
wx.showToast({
title: '上传失败',
icon: 'loading',
duration: 2000
})
}
})
console.log("完成");
wx.showToast({
title: '识别完成',
icon: 'loading',
duration: 2000
})
}
})
}
我的还是没有解决掉,谁有好的例子,能否一看
个人实操经验,主要还是后端的问题,前端只要按照例子copy然后加点自己的data数据就行了。
我的成功案例
upload_face:function()
{
var _this=this;
wx.chooseImage({
success: function(res) {
wx.showToast({
title: '加载中',
icon: 'loading',
duration: 10000
})
var tempFilePaths = res.tempFilePaths;
wx.uploadFile({
url: 'https://upload.****.com/miniapp/image',
filePath: tempFilePaths[0],
name: 'file',
formData:{
uid: lib.getuid()
},
success: function(res)
{
//do something
wx.showModal({
title: '提示',
content: '上传成功'
})
},
fail:function()
{
wx.showModal({
title: '提示',
content: '上传失败'
})
}
})
}
})
},
您看您的小程序中的服务器域名有没有设置上传对应的域名,很多时候会忽略这一点.
单图片很容易,多图片就麻烦些了.
首先wx.chooseImage获取tempFilePaths 之后调用 wx.uploadFile就OK 功能是好用的