upload.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //多张图片上传
  2. function uploadimg(data) {
  3. var that = this,
  4. i = data.i ? data.i : 0,
  5. success = data.success ? data.success : 0,
  6. fail = data.fail ? data.fail : 0;
  7. wx.uploadFile({
  8. url: data.url,
  9. filePath: data.path[i],
  10. name: 'fileData',//这里根据自己的实际情况改
  11. formData: {
  12. title:data.info.num,
  13. desc:data.info.desc,
  14. userid: data.userid
  15. },
  16. success: (resp) => {
  17. success++;
  18. console.log(resp)
  19. console.log(i);
  20. //这里可能有BUG,失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时,这里的success才+1
  21. },
  22. fail: (res) => {
  23. fail++;
  24. console.log('fail:' + i + "fail:" + fail);
  25. },
  26. complete: () => {
  27. console.log(i);
  28. i++;
  29. if (i == data.path.length) { //当图片传完时,停止调用
  30. console.log('执行完毕');
  31. console.log('成功:' + success + " 失败:" + fail);
  32. wx.hideLoading();
  33. wx.showToast({
  34. title: '上传完成',
  35. })
  36. setTimeout(()=>{
  37. wx.navigateBack({
  38. delta: 1 // 回退前 delta(默认为1) 页面
  39. })
  40. }, 2000)
  41. } else {//若图片还没有传完,则继续调用函数
  42. console.log(i);
  43. data.i = i;
  44. data.success = success;
  45. data.fail = fail;
  46. that.uploadimg(data);
  47. }
  48. }
  49. });
  50. }
  51. module.exports = {
  52. uploadimg: uploadimg
  53. }