微信小程式 wx.uploadFile在安卓手機上面the same task is working問題解決_JavaScript

來源:互聯網
上載者:User

微信小程式 wx.uploadFile在安卓手機上面the same task is working問題解決

微信小程式上傳圖片的時候,如果是多圖片上傳,一般都是直接用一個迴圈進行wx.uploadFile

這個在電腦上面測試與蘋果手機上面都不會有什麼問題

但當用安卓測試的時候,你會發現小程式會提示一個the same task is working

wx.uploadFile不能並行,因為wx.uploadFile是一個非同步函數,所以迴圈的時候在安卓手機上會出現並行

解決的方法

做一個上傳完的標識,然後

用遞迴演算法進行上傳

在上傳成功的回呼函數裡面,直接遞迴,標識滿足直接跳出,完成所有圖片上傳

貼上個程式碼片段

//上傳標識var i=0//imglist為要上傳圖片的路徑數組uploadImg: function () {    var that = this  if (i == imglist.length) {  //清空還原   news = ""   city = ""    i=0   wait = true   imglist = []   serverImg = []   retrunList = []   that.setData({    loding: false,    src: [],    disabled: false   })   return;  }   var imgcount = imglist.length;   wx.uploadFile({    url: config.serverUrl('index.php/user/uploadtu'),    filePath: imglist[i],    name: 'file',    formData: { 'user': 'test' },    success: function (res) {     serverImg.push(res.data)     if (imgcount == serverImg.length) {      var serverImgStr = serverImg.join("|")      wx.request({       url: config.serverUrl('index.php/user/baobeiadd'),       method: 'POST',       data: Util.json2Form({        imglist: serverImgStr,        userId: userId,        news: news,        city: city,        latitude: latitude,        longitude: longitude       }),       header: {        'content-type': 'application/x-www-form-urlencoded'       },       success: function (res2) {        if (res2.data.state == "ok") {          that.setData({          loding: false,          butTxt: "發布",          disabled: false         })                   Util.mesUrl("發布成功!", "../index/index")        }        else {         Util.mes("描述至少10人字以上哦,還有圖片也要選哦!")        }       }      })     }else     {     //這裡直接遞迴     i++;     that.uploadImg();     }    },    fail: function (e) {     console.log(e)     Util.mes("圖片上傳失敗,請重新發布!" + i)    }   })  },

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.