//string转为blob
const blob = new Blob(stringVlaue], {type: 'text/plain;charset=utf-8'})
//base64转为string
string=btoa(encodeURI(data))
base64=atob(string)
//blob转base64
const blob = '.....'
const a = new FileReader();
a.readAsDataURL(blob);
a.onload = (e) => {
const base64 = e.target.result
}
base64ToArrayBuffer (base64) {
var arr = base64.split(',')
var binaryString = window.atob(arr[1])
var len = binaryString.length
var bytes = new Uint8Array(len)
for (var i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i)
}
console.log(bytes.buffer)
return bytes.buffer
}
//BlobUrl转blob数据
function objectURLToBlob(url, callback) {
var http = new XMLHttpRequest();
http.open("GET", url, true);
http.responseType = "blob";
http.onload = function (e) {
if (this.status == 200 || this.status === 0) {
callback(this.response)
}
};
http.send()
};
//blob数据转file
objectURLToBlob(url, function (blob) {
let files = new window.File([blob], 'file.name', {type: 'file.type'})
console.log(files)
})
base64ToByte (base64) {
var arr = base64.split(',')
var str = window.atob(arr[1])
var bytes = []
var len, c
len = str.length
for (var i = 0; i < len; i++) {
c = str.charCodeAt(i)
if (c >= 0x010000 && c <= 0x10FFFF) {
bytes.push(((c >> 18) & 0x07) | 0xF0)
bytes.push(((c >> 12) & 0x3F) | 0x80)
bytes.push(((c >> 6) & 0x3F) | 0x80)
bytes.push((c & 0x3F) | 0x80)
} else if (c >= 0x000800 && c <= 0x00FFFF) {
bytes.push(((c >> 12) & 0x0F) | 0xE0)
bytes.push(((c >> 6) & 0x3F) | 0x80)
bytes.push((c & 0x3F) | 0x80)
} else if (c >= 0x000080 && c <= 0x0007FF) {
bytes.push(((c >> 6) & 0x1F) | 0xC0)
bytes.push((c & 0x3F) | 0x80)
} else {
bytes.push(c & 0xFF)
}
}
return bytes
},
dataURLtoBlob(dataurl) {
var arr = dataurl.split(',');
//注意base64的最后面中括号和引号是不转译的
var _arr = arr[1].substring(0,arr[1].length-2);
var mime = arr[0].match(/:(.*?);/)[1],
bstr =atob(_arr),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], {
type: mime
});
},
base64toBlob: function(base64Data) {
var contentType = base64Data.split(',')[0].split(':')[1].split(';')[0];
var sliceSize = 1024;
var byteCharacters = atob(base64Data.substring(base64Data.indexOf(',') + 1));
var bytesLength = byteCharacters.length;
var slicesCount = Math.ceil(bytesLength / sliceSize);
var byteArrays = new Array(slicesCount);
for (var sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
var begin = sliceIndex * sliceSize;
var end = Math.min(begin + sliceSize, bytesLength);
var bytes = new Array(end - begin);
for (var offset = begin, i = 0; offset < end; ++i, ++offset) {
bytes[i] = byteCharacters[offset].charCodeAt(0);
}
byteArrays[sliceIndex] = new Uint8Array(bytes);
}
return new Blob(byteArrays, {
type: contentType
});
},