Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
liaosimin opened this issue Jun 16, 2018 · 3 comments
Open

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

liaosimin opened this issue Jun 16, 2018 · 3 comments

Comments

@liaosimin
Copy link

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

@gpake
Copy link
Owner

gpake commented Jun 19, 2018 via email

@1c7
Copy link

1c7 commented Dec 29, 2018

例子代码

刚好看到这个问题,我把自己项目里上传多张图的代码拿过来。供参考

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
Copy link

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

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants