云开发 多图上传 进度无法对应?
使用云开发上传多个图片,希望每个图片独立显示上传进度,但是进度永远只在图片数组最后一个的里面更新,这是为什么?
data: {
// ...
uploadImages: [],
}
user.chooseImage({
count: count,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
})
.then(res=>{
// 开始遍历
for (var index in res.tempFilePaths) {
var array = {
'path': res.tempFilePaths[index],
'progress': 0
}
that.data.uploadImages[index] = array
const filePath = that.data.uploadImages[index].path
const cloudPath = 'post-images_' + app.globalData.openid + '_' + common.getUniqueCode(new Date()) + '_' + index + filePath.match(/\.[^.]+?$/)[0]
var uploadTask = wx.cloud.uploadFile({
cloudPath,
filePath,
success: res => {
console.log('上传成功', res.fileID)
}
})
uploadTask.onProgressUpdate((res) => {
// 把进度数据放入uploadImages数组
that.data.uploadImages[index].progress = res.progress
// 打印结果永远是最后一个数组对象更新progress,前几个progress一直都是0
console.log('progress: ', that.data.uploadImages)
that.setData({
uploadImages: that.data.uploadImages
})
})
}
})