微信小程序上传图片(限制图片大小、张数)
代码:
//选择图片 choice: function () { var that = this console.log(that.data.imgsrc.length); if (that.data.imgsrc.length <= 3) { var ino = 4 - that.data.imgsrc.length; wx.chooseImage({ //从本地相册选择图片或使用相机拍照 count: ino, sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: (res) => { var tempFilePaths = res.tempFilePaths for (var item in res.tempFilePaths) { console.log(res.tempFilePaths[item]); that.data.imgsrc.push(res.tempFilePaths[item]); tempFilePaths = that.data.imgsrc; console.log(that.data.imgsrc); } let size = res.tempFiles.every(item => { //限制上传图片大小为2M,所有图片少于2M才能上传 return item.size <= 2000000 }) if (size) { that.setData({ imgsrc: tempFilePaths }) }else{ wx.showToast({ title:'上传图片不能大于2M!', icon:'none' }) } } }) } else { wx.showToast({ title: "最多只能上传4张图片", image: "/images/icon-warning.png", }); } },