图片url转base64

var xhr = new XMLHttpRequest()
// 配置的代理,解决跨域问题
xhr.open('GET', url.replace('http://xxx.com', '/img'), true)
xhr.responseType = 'blob'
xhr.onload = function () {
  if (xhr.status === 200) {    
    var reader = new FileReader()
    reader.readAsDataURL(xhr.response)
    reader.onloadend = function () {
      var base64data = reader.result.replace(/^data:image\/(png|jpg);base64,/, "")  
    }   
  }
}
xhr.send()    

总的来说就是将url先转blob,然后将blob转成base64

 

posted @ 2018-08-10 10:12  Cheap_trick  阅读(316)  评论(0编辑  收藏  举报