微信小程序 多圖異步上傳

在app.js定義app

uploadimg: function(data) {
    var that = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'image',
      formData: {
        commentsId: data.id
      },
      success: function(res) {
        success++;
      },
      fail: function(res) {
        fail++;
      },
      complete: function(res) {
        i++;
        if (i == data.path.length) { //當圖片傳完時,中止調用     
        } else {
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }
      }
    })
}
複製代碼

調用dom

引入var app = getApp()this

app.uploadimg({
    id: res.data.data.id,
    url: app.globalData.domainName + '/ltcommerce/wx/comments/saveimg',
    path: this.data.files
  })
複製代碼
相關文章
相關標籤/搜索