微信小程序 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) } }) },感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答