uniapp中上传多个图片只能通过循环的方式 将图片的地址上传,如果想在图片全部上传成功后执行操作,推荐使用Promise.all
// 上传图片
onUploadImg: function() {
uni.chooseMedia({
count: 6 - this.imgList.length,
mediaType: ['image'],
success: ({
tempFiles
}) => {
//imgList中存储的是图片的本地地址
this.imgList.push(...tempFiles.map(x => x.tempFilePath))
}
})
},
Promise.all(this.imgList.map(path=>{
return new Promise((resolve,reject)=>{
uni.uploadFile({
url:'http://xxxxxxx/upload/image',
filePath:path,
name:'file',
header: {'content-type': 'multipart/form-data'},
success: (res) => {
console.log(JSON.parse(res.data).data,'filepath');
if(JSON.parse(res.data).code == 200){
this.images.push(JSON.parse(res.data).data)
}
resolve(res)
},
fail: (error) => {
// 上传失败处理逻辑
reject(error);
}
})
})
})).then((results)=>{
//成功的回调
}))