代碼:
//選擇圖片 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", }); } },