时间:2019-05-07 来源:小程序工厂
付***:
uploadFile: function(successCb, index) {
var that = this;
wx.chooseImage({
count: 1, // 默认9
sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function(res) {
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
var tempFilePaths = res.tempFilePaths;
that.uploadImgFile(that, tempFilePaths, successCb, index)
}
})
},
uploadImgFile: function(page, path, successCb, index) {
wx.showLoading({
title: '上传中...',
mask: true
})
wx.uploadFile({
url: ‘接口地址',
filePath: path[0],
name: 'image',
header: {
"chartset": "utf-8",
"content-type": "multipart/form-data"
},
formData: {
'token': this.globalData.userInfo.token
},
success: function(res) {
if (res.statusCode !== 200) {
wx.showModal({
title: '提示',
content: '上传失败,请稍后重试',
showCancel: false
})
return
} else {
let data = JSON.parse(res.data)
try {
let data = JSON.parse(res.data)
if (data.code === 0) {
console.log(index)
successCb && successCb(data.data, index)
} else {
wx.showModal({
title: '提示',
content: res.msg || '上传失败,请稍后重试',