gpake / qiniu-wxapp-sdk

qiniu SDK based on wxapp
GNU General Public License v3.0
703 stars 278 forks source link

可以批量上传图片吗? #42

Open liaosimin opened 6 years ago

liaosimin commented 6 years ago

比如一次性选了5张图片,我得循环调那个上传接口?

gpake commented 6 years ago

暂时来说,多图是需要这样上传的

1c7 commented 5 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)
}
xfangfang commented 5 years ago

我也放一下我的批量上传代码吧

全部上传结束后会返回图片链接列表

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,
  })
})