<!--pages/test/test.wxml-->
<view>
<view bindtap="upImg">上传</view>
</view>
![点击并拖拽以移动]()
const maxUpImgs = 9
let upCpImg = []
Page({
data: {
},
upImg: function () {
const _this = this
if (upCpImg.length >= maxUpImgs) {
return wx.showToast({
title: `最大数量不能超过${maxUpImgs}`,
icon: 'none',
duration: 2000
})
}
wx.chooseImage({
count: maxUpImgs,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: function (res) {
var tempFilePaths = res.tempFilePaths
_this.up2Server(tempFilePaths)
}
})
},
async up2Server(tempFilePaths) {
const _this = this
let imageData = await _this.upload(tempFilePaths)
console.log(imageData)
},
upload: (pathArr) => {
wx.showToast({
icon: "loading",
title: "正在上传"
})
let index = 0
const maxlength = pathArr.length
let success = 0
let fail = 0
let outArr = []
let outArrShow = []
return new Promise(function (resolve, reject) {
const up = () => {
wx.showToast({
icon: "loading",
title: `正在上传(${index + 1}/${maxlength})`
})
wx.uploadFile({
url: "",
filePath: pathArr[index],
header: {
"Content-Type": "multipart/form-data"
},
formData: {
params: {}
},
name: 'image',
success: (res) => {
var data = res.data
outArr.push(data)
outArrShow.push(pathArr[index])
success++
},
fail: (res) => {
fail++;
},
complete: () => {
index++
if (index >= maxlength) {
const info = {
success: success,
fail: fail,
file: outArr,
fileShow: outArrShow
}
wx.hideToast()
resolve(info)
} else {
up();
}
}
});
}
up()
})
}
})
![点击并拖拽以移动]()