阅读 446

图片URL地址转base64

var getBase64ByUrl = function(src, callback, outputFormat) {
   // console.log()   
let newurl;  
 if(src.indexOf('http://files.xinan.zhanyaa.com')!=-1){     
 newurl=src.replace('http://files.xinan.zhanyaa.com','https://xinmedia.oss.aliyuncs.com') 
  }else {      newurl=src   }   
var xhr = new XMLHttpRequest(); 
  xhr.open('GET', newurl, true);  
 xhr.responseType = 'arraybuffer';  
 xhr.onload = function(e) {      
if (xhr.status == 200) {       
  var uInt8Array = new Uint8Array(xhr.response);     
    var i = uInt8Array.length;       
  var binaryString = new Array(i);      
   while (i--) {         
   binaryString[i] = String.fromCharCode(uInt8Array[i]);     
    }       
  var data = binaryString.join('');    
     var base64 = window.btoa(data);  
       var dataUrl = "data:" + (outputFormat || "image/png") + ";base64," + base64;     
    callback.call(this, dataUrl);      }   };
   xhr.send();}
复制代码

文章分类
前端
文章标签