话不多说,直接上代码:
<一>base64 转 blob
1,通过第三方包:b64-to-blob
import b64ToBlob from 'b64-to-blob'
public base64ToBlob(base64: string): Promise<Blob> {
return new Promise((resolve, reject) => {
try {
let arr = base64.split(',');
let mime = arr[0].match(/:(.*?);/)[1]
resolve(b64ToBlob(arr[1], mime))
} catch (error) {
reject(error)
}
})
}
2,通过js window下的方法atob
function dataURLtoBlob(dataurl) {
let arr = dataurl.split(',')
let mime = arr[0].match(/:(.*?);/)[1]
let bstr = atob(arr[1])
let n = bstr.length
let u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new Blob([u8arr], { type: mime })
}
<二> blob 转 base64
public blobToBase64(blob: Blob): Promise<string> {
return new Promise((resolve) => {
try {
let fileReader = new FileReader()
fileReader.readAsDataURL(blob)
fileReader.onloadend = function(e) {
let base64 = e.target.result
resolve(base64.toString())
};
} catch (error) {
resolve("")
}
});
}
<三> string 转blob
const blob = new Blob(stringVlaue], {type: 'text/plain;charset=utf-8'})
<四> string 转 base64
1. 通过js window下的方法btoa
const stringValue = 'helle'
const base64 = btoa(stringValue)
const stringData = atob(base64) // 这里拿到的是ASCII的编码,需拿到Unicode编码要再次转换成相应的编码
2.通过以下编码方法进行编码
const Base64 = {
// private property
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
// public method for encoding
encode: function(input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = Base64._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
}
return output;
},
// private method for UTF-8 encoding
_utf8_encode: function(string) {
string = string.replace(/\r\n/g, "\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} else if ((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
} else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
}
}
const stringValue = 'abcdef'
const base64 = Base64.encode(stringValue)
<五> base64转string
1. 通过js window下的方法atob
const stringData = atob(base64) // base64必需,是一个通过 btoa() 方法编码的字符串(base64支字符串)
2.通过以下编码方法进行编码
const Base64 = {
// private property
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
// public method for decoding
decode (input) {
let output = ''
let chr1 = ''
let chr2 = ''
let chr3 = ''
let enc1 = ''
let enc2 = ''
let enc3 = ''
let enc4 = ''
let i = 0
// eslint-disable-next-line
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '')
while (i < input.length) {
enc1 = this._keyStr.indexOf(input.charAt(i++))
enc2 = this._keyStr.indexOf(input.charAt(i++))
enc3 = this._keyStr.indexOf(input.charAt(i++))
enc4 = this._keyStr.indexOf(input.charAt(i++))
chr1 = (enc1 << 2) | (enc2 >> 4)
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
chr3 = ((enc3 & 3) << 6) | enc4
output = output + String.fromCharCode(chr1)
if (enc3 !== 64) {
output = output + String.fromCharCode(chr2)
}
if (enc4 !== 64) {
output = output + String.fromCharCode(chr3)
}
}
output = this.utf8Decode(output)
return output
},
// private method for UTF-8 decoding
utf8Decode (utftext) {
// 去除转换后的字符串无用的前缀'u«ZµìmþZvÚ±î¸' 只保留后面实际的文本部分
utftext = utftext.split('î¸')[1]
let string = ''
let i = 0
let c = 0
let c2 = 0
let c3 = 0
while (i < utftext.length) {
c = utftext.charCodeAt(i)
if (c < 128) {
string += String.fromCharCode(c)
i++
} else if ((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i + 1)
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63))
i += 2
} else {
c2 = utftext.charCodeAt(i + 1)
c3 = utftext.charCodeAt(i + 2)
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63))
i += 3
}
}
return string
}
}
const file // file必须是base64编码的数据格式
const string= Base64.decode(file)
<六> blob转blob url
window.URL.createObjectURL(blob)