一、页面WXML
<view class="weui-uploader__bd"> <view class="weui-uploader__files"> <block wx:for="{{imageList}}" wx:for-item="image"> <view class="weui-uploader__file"> <image class="weui-uploader__img" src="{{image}}" data-src="{{image}}" bindtap="previewImage"></image> </view> </block> </view> <view class="weui-uploader__input-box"> <view class="weui-uploader__input" bindtap="chooseImage"></view> </view> </view> </view> <view class="l-btn" bindtap="upload">发布需求</view>
二、选择图片
data:{
imageList:[]
},
chooseImage: function () { var that = this wx.chooseImage({ count: 6, success: function (res) { that.setData({ imageList: res.tempFilePaths }) } }) },
三、上传
小程序一次只能上传一张图片,所以写了一个递归去上传,里面的判断没好好写,大概就是个思路,欢迎拍砖
upload:function(){ var i = 0, demand_id=\'\',uploadFiles = function(){ // console.log(i,demand_id) if (i == self.data.imageList.length) { sig = false; wx.redirectTo({ url:\'../square/square?demand_id=\' + demand_id }); return; } wx.uploadFile({ url: app.globalData.ajaxurl + \'api/demand/push\', filePath: self.data.imageList[i], // 通过chooseImage获取到的文件临时列表 name: \'file\', //文件键名(后台通过此名字获得文件数据) formData: formdata, //二外数据 complete:function(res){ if(typeof res.data == \'string\'){ res.data = JSON.parse(res.data) } i++; if (res.statusCode == 200) { if (res.data.status == 200 && i==1) { formdata.demand_id = res.data.data.demand_id; if(!demand_id){ demand_id = formdata.demand_id; // console.log(demand_id) } uploadFiles(); } }else{ wx.showModal({ title: \'失败\', content: \'请求失败,请重试\' }) sig = false; return; } if(i>1){ uploadFiles(); } } }); } uploadFiles(); } }