图片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

 

转载于:https://www.cnblogs.com/CheapTrick/p/9453363.html

你可能感兴趣的:(图片url转base64)