//多张图片上传 function uploadimg(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: 'fileData',//这里根据自己的实际情况改 formData: { title:data.info.num, desc:data.info.desc, userid: data.userid }, success: (resp) => { success++; console.log(resp) console.log(i); //这里可能有BUG,失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时,这里的success才+1 }, fail: (res) => { fail++; console.log('fail:' + i + "fail:" + fail); }, complete: () => { console.log(i); i++; if (i == data.path.length) { //当图片传完时,停止调用 console.log('执行完毕'); console.log('成功:' + success + " 失败:" + fail); wx.hideLoading(); wx.showToast({ title: '上传完成', }) setTimeout(()=>{ wx.navigateBack({ delta: 1 // 回退前 delta(默认为1) 页面 }) }, 2000) } else {//若图片还没有传完,则继续调用函数 console.log(i); data.i = i; data.success = success; data.fail = fail; that.uploadimg(data); } } }); } module.exports = { uploadimg: uploadimg }