How does Mini Program upload multiple pictures at the same time?

now, if you upload 9 photos, you have to send them to the server nine times, and the server has to wait for all of them to be uploaded before it can be processed. Is there a way for Mini Program to upload more than one picture at a time?


it is obvious that Wechat only provides one way to upload a picture, and it is uploaded by a picture.
but the principle of uploading multiple images can be multiple uploads of one picture.
you can use Promise.all to encapsulate uploaded tasks. Return to

after all processing.
var files=[1,2,3]
var uploads=[];
for(var i=0;i<files.length;iPP){
          uploads[i]=new Promise((resolve,reject)=>{
            wx.uploadFile({
                  url: 'https://example.weixin.qq.com/upload', //
                  filePath: files[i],
                  success: function(res){
                    resolve(res)
                  }
                })
          })
          
        }
        
        Promise.all(uploads).then((result)=>{
            resolve(result)
          })

Bindtap method on

element: (select picture)
chooseImgHandler: function (e) {

var that = this;
that.count_img = count_img; //
if (that.isGoing) return;
that.isGoing = true;
if (count_img) {
  wx.chooseImage({
    count: count_img,
    sizeType: ['compressed'],
    sourceType: ['album', 'camera'],
    success: function (res) {
      var tempFilePaths = res.tempFilePaths;
      wx.showLoading();
      that.uploadimg(tempFilePaths);
    },
    fail: function () {
      // App.serverErrorTip("...");
      that.isGoing = false;
    }
  })
} else {
  App.serverErrorTip('~');
  that.isClicking = false;
}

},
/ / after getting the temporary file path array of the picture selected by the user, upload it to your own server
/ / upload multiple images
uploadimg: function (data) {

var that = this,
  zero = 0,
  i = data.i ? data.i : zero,
  success = data.success ? data.success : zero,
  fail = data.fail ? data.fail : zero,
  count_img = that.count_img;
wx.uploadFile({
  url: '',
  filePath: data[i],
  name: 'file',
  formData: {},
  success: function (resp) {
    if (resp.statusCode == 200) {
      if (!resp.data || resp.data.indexOf("errcode") != -1) {
        var msg = "";
        try {
          var json = eval("(" + resp.data + ")");
          msg = ':' + json.errmsg;
        } catch (e) {
          msg = ''
        }
        that.wetoast.toast({
          title: msg,
          duration: 3000
        })
        failPP;
        wx.hideLoading();
      } else {
       //
       //
        that.count_img = count_img;
        if (i == (data.length - 1)) {
          wx.hideLoading();
        }
      }
    } else {
      if (i == (data.length - 1)) {
        wx.hideLoading();
      }
      App.serverErrorTip('');
    }
  },
  fail: function (res) {
    failPP;
    if (i == (data.length - 1)) {
      wx.hideLoading();
    }
  },
  complete: function () {
    iPP;
    if (i == data.length) {   //          
      console.log('');
      console.log(':' + success + " :" + fail);
      that.isClicking = false;
    } else {//
      data.i = i;
      data.success = success;
      data.fail = fail;
      that.uploadimg(data);
    }
  }
});

},


I use
let img_upload = (url, path) = > {
return new Promise ((resolve, rej) = > {

wx.uploadFile({
  url: url,
  filePath: path,
  name: 'filename',
  success: function (res) { resolve(res) },
  fail: function () {
    wx.showToast({
      title: '',
      icon: 'none'
    })
  }
})

})
}

use
img (). Then ((res) = > {

res.tempFilePaths.forEach(el=> {
}

})


how do you do it


what about multiple formData submissions? (upload N pictures and add N records to the database)

Menu