up_img: function() {
let that = this;
wx.chooseImage({
count: 1, // 默认9
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function(res) {
console.log('res', res);
wx.showToast({
title: '正在上传...',
icon: 'loading',
mask: true,
duration: 1000
});
var tempFilePaths = res.tempFilePaths;
that.setData({
head_img: tempFilePaths[0]
});
console.log('tempFilePaths[0]', tempFilePaths[0]);
const uploadTask = wx.uploadFile({
url: httpUrl2 + '5a63444c41b45',
filePath: tempFilePaths[0],
name: 'file',
header: {
"Content-Type": "multipart/form-data"
},
formData: {
//'file':
},
success: function(res) {
var data = JSON.parse(res.data);
console.log("data1", data);
console.log("data1", typeof(data.data.Url));
that.setData({
head_img: data.data.Url
});
//do something
},
fail: function(res) {
console.log("res", res);
}
});
uploadTask.onProgressUpdate((res) => {
console.log('上传进度', res.progress)
console.log('已经上传的数据长度', res.totalBytesSent)
console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
})
},
fail: function(res) {
console.log("接口调用失败");
}
})
},
这里我想说一下,小程序上传头像的后端写法跟公众号之类的的不同,如果上传不成功让后端看看小程序开发文档吧。