微信小程序 wx.uploadFile在安卓手機上面the same task is working問題解決
微信小程序 wx.uploadFile在安卓手機上面the same task is working問題解決
微信小程序上傳圖片的時候,如果是多圖片上傳,一般都是直接用一個循環(huán)進行wx.uploadFile
這個在電腦上面測試與蘋果手機上面都不會有什么問題
但當用安卓測試的時候,你會發(fā)現(xiàn)小程序會提示一個the same task is working
wx.uploadFile不能并行,因為wx.uploadFile是一個異步函數(shù),所以循環(huán)的時候在安卓手機上會出現(xiàn)并行
解決的方法
做一個上傳完的標識,然后
用遞歸算法進行上傳
在上傳成功的回調(diào)函數(shù)里面,直接遞歸,標識滿足直接跳出,完成所有圖片上傳
貼上個代碼段
//上傳標識 var i=0 //imglist為要上傳圖片的路徑數(shù)組 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: "發(fā)布", disabled: false }) Util.mesUrl("發(fā)布成功!", "../index/index") } else { Util.mes("描述至少10人字以上哦,還有圖片也要選哦!") } } }) }else { //這里直接遞歸 i++; that.uploadImg(); } }, fail: function (e) { console.log(e) Util.mes("圖片上傳失敗,請重新發(fā)布!" + i) } }) },
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
Flutter刷新組件RefreshIndicator自定義樣式demo
這篇文章主要介紹了Flutter刷新組件RefreshIndicator自定義樣式demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02ECharts框架Sunburst拖拽功能實現(xiàn)方案詳解
這篇文章主要為大家介紹了ECharts框架Sunburst拖拽功能實現(xiàn)方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12JavaScript+HTML實現(xiàn)學生信息管理系統(tǒng)
這篇文章主要介紹了JavaScript實現(xiàn)學生信息管理系統(tǒng),文中有非常詳細的代碼示例,對正在學習js的小伙伴們有一定的幫助,需要的朋友可以參考下2021-04-04微信小程序 wxapp內(nèi)容組件 progress詳細介紹
這篇文章主要介紹了微信小程序 wxapp內(nèi)容組件 progress詳細介紹的相關資料,需要的朋友可以參考下2016-10-10