上传图片

    // 上传头像
    uploadPhoto (e) {
      let fileObj = e.target.files[0]
      let reader = new FileReader()
      reader.readAsDataURL(fileObj)
      reader.onloadend = e => {
        let result = e.target.result
        this.headPortraitSrc = result
        this.defalutPhotoVisible = false
        if (result.size <= 200 * 1024) {
          // 小于两兆直接上传
        } else {
          // 大于两兆压缩后上传
          let imgObj = e.target
          imgObj.onload = () => {
          }
          console.log()
        }
      }
    },
    // 压缩图片
    compressImg (img) {
      let canvas = document.createElement('canvas')
      let ctx = canvas.getContext('2d')
      canvas.width = img.width
      canvas.height = img.height
      ctx.fillStyle = '#fff'
      ctx.fillRect(0, 0, canvas.width, canvas.height)
      ctx.drawImage(img, 0, 0, img.width, img.height)
      return canvas.toDataURL('image/jpeg', 0.5)
    },
    // [transformBaseToBlob base64转成bolb对象]
    transformBaseToBlob (base64Data) {
      let arr = base64Data.split(',')
      let mimeType = arr[0].match(/:(.*?);/)[1]
      let bstr = atob(arr[1])
      let len = bstr.length
      let u8arr = new Uint8Array(len)
      while (len--) {
        u8arr[len] = bstr.charCodeAt(len)
      }
      return new Blob([u8arr], { type: mimeType })
    },

  

posted on 2019-01-25 11:02  木之子梦之蝶  阅读(193)  评论(0编辑  收藏  举报

导航