微信小程序获取头像昵称,限制头像大小

把用户的头像上传,限制在2M以内 

代码


  that.onChooseAvatar = (e) => {
    console.log('onChooseAvatar2', e);
    const avatarUrl = e.target.avatarUrl;

    let FileSystemManager = wx.getFileSystemManager()
    FileSystemManager.getFileInfo({
      "filePath": avatarUrl,
      success(res) {
        console.log('FileInfo-res', res)
        if (res.size > 1024 * 1024 * 2) {
          wx.showToast({
            title: '所选图片过大,请选择2M以内的图片',
            icon: 'none'
          })
        } else {
          let imgPath =
            `nhs/userUpload/avatarUrl/${wx.getStorageSync('userToken')}${new Date().getTime() + Math.floor(Math.random() * 150)}`
          uploadImage.uploadFile(
            avatarUrl, imgPath,
            function(result) {
              that.saveUserInfo({
                'avatarUrl': result
              })
            },
            function(result) {
              wx.hideLoading();
            }
          )
        }
      },
      fail(err) {
        console.log('FileInfo-err', err)
      },
    })


  }

猜你喜欢

转载自blog.csdn.net/qq_35713752/article/details/128005602
今日推荐