技术前置:
1.框架采用colorUI
2.原生开发
功能:
上传图片
1.上传已经拍摄的图片
2.实时拍摄上传
3.设置上传图片数量,每次上传数量
4.上传等待
ChooseImage() {if(this.data.imgList.length>=4){_this.ErrorEvent("最多上传4张照片");return;}if(this.data.imgList.length>=4){_this.ErrorEvent("最多上传4张照片");return;}if(this.data.Mylatitude==null){this.ErrorEvent("请点击定位送达地址!");return;}if(this.data.Mylongitude==null){this.ErrorEvent("请点击定位送达地址!");return;}var position=this.data.Mylatitude+","+this.data.Mylongitude;wx.chooseImage({count: 1, //默认9sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有sourceType: ['album','camera'], //从相册选择 ,success: (res) => {if(res.tempFilePaths.length<=0){return;}wx.showLoading({title: '正在上传图片',// icon: 'loading',// duration: 5000,mask: true})const tempFilePaths = res.tempFilePaths;var uploadimgs=[];for(var i=0;i<tempFilePaths.length;i++){this.uploadImg(tempFilePaths[i],position);}}});},//上传图片uploadImg(address,longlatData){let reqaddress= app.globalData.baseUrl+ '/uploadPic';let _this=this;wx.uploadFile({url: reqaddress, //仅为示例,非真实的接口地址filePath: address,name: 'file',formData: {'user': 'test'},success (res){const data = res.data;var mdata= JSON.parse(data);wx.hideLoading();if(mdata.code==0){if (_this.data.uploadImgList.length > 0) {_this.setData({uploadImgList:_this.data.uploadImgList.concat(mdata.data.relativePath),})} else {var show="uploadImgList[0]";_this.setData({[show]:mdata.data.relativePath})}if (_this.data.imgList.length != 0) {_this.setData({imgList: _this.data.imgList.concat(address),})} else {var show="imgList[0]";_this.setData({[show]: address,})}_this.HideErrorEvent();wx.showToast({title: '图片上传成功',duration: 1000,})}else{// var count=_this.data.imgList.length;// _this.setData({// imgList:_this.data.imgList.splice(count-1, 1)// })_this.ErrorEvent("上传图片到服务器失败!请重传");}}, complete() { },fail(error){wx.hideLoading();console.log(error);// var count=_this.data.imgList.length;// _this.setData({// imgList:_this.data.imgList.splice(count-1, 1)// })_this.ErrorEvent("上传图片到服务器失败!请重传");}})},