小程序获取base64值

小程序默认获取的是临时图片地址, 长这样, http://tmp/wx8a62ca951fd0b7af.oxxxxxx.png

虽然可以上传, 但是有时候是集成的, 比如IM, 这时候需要base64、blob类型, 需要转换下。

wx.chooseImage({
      count: 1, // 最多可以选择的图片张数,默认9
      sizeType: ['original', 'compressed'], // original 原图,compressed 压缩图,默认二者都有
      sourceType: ['album'], // album 从相册选图,camera 使用相机,默认二者都有
      success: function(res){
        var path = res.tempFilePaths[0]
        console.log(res)
        console.log(path)
        wx.request({
              url: path,
              method: 'GET',
              responseType: 'arraybuffer',
              success: (res) => {
                let base64 = wx.arrayBufferToBase64(res.data);
                let userImageBase64 = 'data:image/jpg;base64,' + base64;
                console.log('----base64----')
                console.log(userImageBase64)
              }
        })
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })

直接请求临时地址,在响应的时候要求返回arraybuffer类型, 卧槽, 感觉好机智阿。。

--END--

你可能感兴趣的:(小程序获取base64值)