1. 程式人生 > >js blob流和base64,以及file和base64的相互轉換

js blob流和base64,以及file和base64的相互轉換

js blob流和base64,以及file和base64的相互轉換

file和base64

1.file檔案轉換為base64,得到base64格式圖片

var reader = new FileReader();
reader.readAsDataURL(this.files[0]);
reader.onload = function(){
    console.log(reader.result); //獲取到base64格式圖片
};

2.base64轉換為file

function dataURLtoFile(dataurl, filename) {//將base64轉換為檔案
  var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
      bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  while(n--){
      u8arr[n] = bstr.charCodeAt(n);
  }
  return new File([u8arr], filename, {type:mime});
}    

測試案例:

<input type="file" name="" id="imgfile">
<script>
  var base64Img = '';
  document.getElementById('imgfile').onchange = function(){
      var fileReader = new FileReader();
      fileReader.readAsDataURL(this.files[0]);
      fileReader.onload = function(){
          base64Img = fileReader.result;
          console.log(dataURLtoFile(base64Img,'img11'))
      }
  }
   function dataURLtoFile(dataurl, filename) {//將base64轉換為檔案
      var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
          bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
      while(n--){
          u8arr[n] = bstr.charCodeAt(n);
      }
      return new File([u8arr], filename, {type:mime});
  }    
</script>
blob和base64

3.base64轉換為blob流

function dataURItoBlob(base64Data) {
    //console.log(base64Data);//data:image/png;base64,
    var byteString;
    if(base64Data.split(',')[0].indexOf('base64') >= 0)
        byteString = atob(base64Data.split(',')[1]);//base64 解碼
    else{
        byteString = unescape(base64Data.split(',')[1]);
    }
    var mimeString = base64Data.split(',')[0].split(':')[1].split(';')[0];//mime型別 -- image/png

    // var arrayBuffer = new ArrayBuffer(byteString.length); //建立緩衝陣列
    // var ia = new Uint8Array(arrayBuffer);//建立檢視
    var ia = new Uint8Array(byteString.length);//建立檢視
    for(var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }
    var blob = new Blob([ia], {
        type: mimeString
    });
    return blob;
}  

4.blob流轉換為base64

function blobToDataURI(blob, callback) {
   var reader = new FileReader();
   reader.readAsDataURL(blob);
   reader.onload = function (e) {
       callback(e.target.result);
   }
}

測試案例,可直接複製執行

<input type="file" id="imgfile">
<\img src="" id="img" alt=""> //這裡圖片總是轉義,暫且這麼寫,知道是img就行
<\img src="" id="img2" alt="">
<script>
 document.getElementById('imgfile').onchange = function(){
     reads(this.files[0],function(base64Data){   //獲取圖片的base64格式,顯示
         document.getElementById("img").src= reader.result;
         var blob = dataURItoBlob(reader.result); //轉換為blob格式
         blobToDataURI(blob,function(result){    //blob格式再轉換為base64格式
             document.getElementById('img2').src = result;
         })
     });
 }
 function reads(_file,callback){
     var reader = new FileReader();
     reader.readAsDataURL(_file);
     reader.onload = function(){
         callback(reader.result);
     };
 }
 function dataURItoBlob(base64Data) {
     //console.log(base64Data);//data:image/png;base64,
     var byteString;
     if(base64Data.split(',')[0].indexOf('base64') >= 0)
         byteString = atob(base64Data.split(',')[1]);//base64 解碼
     else{
         byteString = unescape(base64Data.split(',')[1]);
     }
     var mimeString = base64Data.split(',')[0].split(':')[1].split(';')[0];//mime型別 -- image/png

     // var arrayBuffer = new ArrayBuffer(byteString.length); //建立緩衝陣列
     // var ia = new Uint8Array(arrayBuffer);//建立檢視
     var ia = new Uint8Array(byteString.length);//建立檢視
     for(var i = 0; i < byteString.length; i++) {
         ia[i] = byteString.charCodeAt(i);
     }
     var blob = new Blob([ia], {
         type: mimeString
     });
     return blob;
 }  

 function blobToDataURI(blob, callback) {
     var reader = new FileReader();
     reader.readAsDataURL(blob);
     reader.onload = function (e) {
         callback(e.target.result);
     }
 }
</