Binary interrotation

1. Switch the file object to base64
 let reader = new FileReader();
 reader.readAsDataURL(file[0])
 console.log(reader)
Copy the code
2. Switch base64 to BLOB for uploading
function dataURItoBlob(dataURI) {  
    var byteString = atob(dataURI.split(', ') [1]);  
    var mimeString = dataURI.split(', ') [0].split(':') [1].split('; ') [0];  
    var ab = new ArrayBuffer(byteString.length);  
    var ia = new Uint8Array(ab);  
    for (var i = 0; i < byteString.length; i++) {  
        ia[i] = byteString.charCodeAt(i);  
    }  
    return new Blob([ab], {type: mimeString});  
}
Copy the code
3. Blob turns into ArrayBuffer
let blob = new Blob([1.2.3.4])
let reader = new FileReader();
reader.onload = function(result) {
    console.log(result);
}
reader.readAsArrayBuffer(blob);
Copy the code
4. Buffer is converted to bloB
let blob = new Blob([buffer])
Copy the code
5. Turn base64 file
const base64ConvertFile = function (urlData, filename) { / / 64 file
  if (typeofurlData ! ='string') {
    this.$toast("UrlData is not a string")
    return;
  }
  var arr = urlData.split(', ')
  var type = arr[0].match(/ : (. *?) ; /) [1]
  var fileExt = type.split('/') [1]
  var bstr = atob(arr[1])
  var n = bstr.length
  var u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new File([u8arr], 'filename.' + fileExt, {
    type: type
  });
}
Copy the code