您好,登錄后才能下訂單哦!
本篇文章為大家展示了利用android進行多圖上傳時出現(xiàn)失敗如何解決,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
微信提供了文件上傳的方法wx.uploadFile來上傳我們的圖片
wx.chooseImage({ success: function(res) { var tempFilePaths = res.tempFilePaths wx.uploadFile({ url: 'http://example.weixin.qq.com/upload', //僅為示例,非真實的接口地址 filePath: tempFilePaths[0], name: 'file', formData:{ 'user': 'test' }, success: function(res){ var data = res.data //do something } }) } })
但是針對多圖上傳微信沒有給出相應的方法來解決,如此我們只能消耗我們程序猿的腦細胞來解決了,最開始我使用了for循環(huán)來循環(huán)上傳我的圖片,恰好本人是蘋果手機所以上傳是沒有問題的,本以為輕松解決了這個問題但是提交到測試以后坑了。測試MM說他那里提示上傳失敗。
于是借來測試手機打印出來錯誤消息
uploadFile:fail:the same task is working
wx.uploadFile不能并行,因為wx.uploadFile是一個異步函數(shù),所以循環(huán)的時候在安卓手機上會出現(xiàn)并行
所以上面的通過循環(huán)wx.uploadFile方法進行多圖上傳肯定是不能行的了,既然不能并行我們是不是可以讓wx.uploadFile執(zhí)行完后再執(zhí)行wx.uploadFile了??聪滦薷暮蟮拇a
這里為上傳圖片的方法,在里面作出判斷上傳完成以后重復調(diào)用upload_img
var img_index = 0;//上傳帶第幾張 var image_list1 = new Array(); //上傳圖片 var upload_img = function (that, file_name) { that.setData({ hidden: false }); wx.uploadFile({ url: '', filePath: file_name, name: 'file', success: function (res) { //此處判斷是否上傳成功 var obj = JSON.parse(res.data); if (obj.ret_code == 1) { //上傳成功以后將上傳成功的圖片加入數(shù)組顯示出來,這樣可以避免沒有上傳成功的圖片就不顯示 var uploads = new Array(); var image_list = new Array(); //加入返回值 uploads = that.data.upload; uploads.push(obj.data); //加入圖片 image_list = that.data.tempFilePaths; image_list.push(file_name); that.setData({ upload: uploads, tempFilePaths: image_list }); //上傳成功一次img_index+1,下面再次調(diào)用upload_img上傳圖片就可以直接傳image_list1[img_index],也就是下一張圖片的鏈接 img_index = img_index + 1; //這里需要作出判斷圖片是否上傳完成,如果完成則取消緩沖框hidden if (img_index < image_list1.length) { upload_img(that, '' + image_list1[img_index]); } else { that.setData({ hidden: true }); } //刷新界面 that.update(); } else { that.setData({ hidden: true }); utils.show_toast(obj.msg); } }, fail: function (res) { that.setData({ hidden: true }); utils.show_toast('加入失敗'); } }) }
選擇圖片方法
if (that.data.tempFilePaths.length < 9) { wx.chooseImage({ count: 9 - that.data.tempFilePaths.length, // 最多可以選擇的圖片張數(shù),默認9 sizeType: ['compressed'], // original 原圖,compressed 壓縮圖,默認二者都有 sourceType: ['album', 'camera'], // album 從相冊選圖,camera 使用相機,默認二者都有 success: function (res) { // success img_index = 0; image_list1=new Array(); //將選擇的圖片放入要上傳的數(shù)組中 for (var i = 0; i < res.tempFilePaths.length; i++) { console.log(i + ';' + res.tempFilePaths[i]); image_list1.push(res.tempFilePaths[i]); } //最開始上傳第一張圖片 upload_img(that, '' + image_list1[img_index]); }, fail: function () { utils.show_toast('選取失敗'); } }) } else { utils.show_toast('當前最多只能選擇9張圖片'); }
通過上面的代碼就可以完成多圖上傳了,這樣也避免了Android手機報錯的漏洞
這里封裝了一個錯誤消息彈窗避免寫重復的代碼
utils.show_toast(‘當前最多只能選擇9張圖片') //彈窗 function show_toast(text) { wx.showToast({ title: text, icon: ‘success', duration: 2000 }); }
上述內(nèi)容就是利用android進行多圖上傳時出現(xiàn)失敗如何解決,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。