const app = getApp()
初始数据: data: { uploadPicList:[] }, 点击上传事件: uploads: function () { var that = this; wx.chooseImage({ success: function (res) { var tempFilePaths = res.tempFilePaths wx.uploadFile({ url: 'https://www.xxx.com/upload.php', filePath: tempFilePaths[0], name: 'file', formData:{ 'user':'cyxxzjp' }, success: function(res){ var dataformat = {imgurl:res.data, title:'上传的图片'} var thispiclist = that.data.uploadPicList thispiclist.push(dataformat) that.setData({ uploadPicList: thispiclist }) } }) }, }) },
删除功能,只有本地,需要增加request到服务器端删除功能
delpic: function(event){ var delimg = event.target.dataset.imgurl var tp = this.data.uploadPicList for(var i=0; i<tp.length; i++){ if (tp[i].imgurl == delimg){ tp.splice(i,1) } } this.setData({ uploadPicList:tp }) }
wxml:
<button bindtap='uploads'>上传文件/图片</button> <view class='uploadpics' wx:if="{{uploadPicList}}"> <view wx:for="{{uploadPicList}}"> <image src='{{item.imgurl}}' mode='aspectFit'></image> <text>{{item.title}}</text> <view bindtap='delpic' data-imgurl="{{item.imgurl}}">删除</view> </view> </view>