Open liaosimin opened 6 years ago
暂时来说,多图是需要这样上传的
刚好看到这个问题,我把自己项目里上传多张图的代码拿过来。供参考
var that = this;
for (var index = 0; index < images_array.length; index++) {
const path = images_array[index]['crop_src'];
(function (path, index, that) {
qiniuUploader.upload(path, (res) => {
// 上传成功了该干啥干啥
}, (error) => {
console.log('error: ' + error);
wx.showToast({
title: '出错了,请稍后再试',
icon: 'none',
})
}, {
region: 'SCN',
domain: 'https://img.xxx.com',
uptokenURL: 'https://xxx.com/backend/api/v1/upload_token_for_wechat',
}, function (p) {})
})(path, index, that)
}
全部上传结束后会返回图片链接列表
let uplist = []
for (let index in this.data.images) {
const path = this.data.images[index]['imagePath'];
let promise = new Promise((resolve, reject) => {
qiniuUploader.upload(path, (res) => {
resolve(res.key)
}, (error) => {
reject(error)
});
})
uplist.push(promise)
}
Promise.all(uplist).then((resultList) => {
console.log(resultList)
},error=>{
console.log(error)
wx.showToast({
title: '图片上传失败',
icon: 'none',
duration: 2000,
})
})
比如一次性选了5张图片,我得循环调那个上传接口?