本文實例為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內容如下/p>
圖片上傳服務器:
wxml
<view class="container"> <button bindtap='chooseImageTap'>上傳圖片</button></view>
wxss
Page({ /** * 頁面的初始數據 */ data: { imgs: [],//本地圖片地址數組 picPaths:[],//網絡路徑 }, /** * 生命周期函數--監聽頁面加載 */ onLoad: function (options) { }, //添加上傳圖片 chooseImageTap: function () { var that = this; wx.showActionSheet({ itemList: ['從相冊中選擇', '拍照'], itemColor: "#00000", success: function (res) { if (!res.cancel) { if (res.tapIndex == 0) { that.chooseWxImage('album') } else if (res.tapIndex == 1) { that.chooseWxImage('camera') } } } }) }, // 圖片本地路徑 chooseWxImage: function (type) { var that = this; var imgsPaths = that.data.imgs; wx.chooseImage({ sizeType: ['original', 'compressed'], sourceType: [type], success: function (res) { console.log(res.tempFilePaths[0]); that.upImgs(res.tempFilePaths[0], 0) //調用上傳方法 } }) }, //上傳服務器 upImgs: function (imgurl, index) { var that = this; wx.uploadFile({ url: 'https://xxxxxxxxxxxxxxxxxxxxxxxxxxxx',// filePath: imgurl, name: 'file', header: { 'content-type': 'multipart/form-data' }, formData: null, success: function (res) { console.log(res) //接口返回網絡路徑 var data = JSON.parse(res.data) that.data.picPaths.push(data['msg']) that.setData({ picPaths: that.data.picPaths }) console.log(that.data.picPaths) } }) }, })
思路很簡單,多張上傳的話,在 upImgs 方法回調做判斷 index++ 繼續調用 upImgs方法即可
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答