微信小程序上傳圖片(限制圖片大小、張數)


代碼:

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM