微信返回的流式数据是 arrayBuffer 格式的
尝试了第一种办法,这种只可以在h5页面上使用,在真机上 decodeURIComponent 这个方法会报一些转化的错误
let resStr = '', chunk = 8 * 1024, i;
const unitStr = new Uint8Array(SSEUnit8ArrayStr[0])
for (i = 0; i < unitStr.length / chunk; i++) {
resStr += String.fromCharCode.apply(null, unitStr.slice(i * chunk, (i + 1) * chunk))
}
resStr += String.fromCharCode.apply(null, unitStr.slice(i * chunk)).replace(/%/g,'%25')
resStr = decodeURIComponent(escape(resStr))
尝试了使用第二种方式(完美解决)
const data = Utf8ArrayToStr(new Uint8Array(str))
console.log(data)
const Utf8ArrayToStr = (array) => {
var out, i, len, c;
var char2, char3;
out = "";
len = array.length;
i = 0;
while(i < len) {
c = array[i++];
switch(c >> 4)
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
out += String.fromCharCode(c);
break;
case 12: case 13:
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
break;
}
}
return out;
}
大佬 这些值打印的时候看着可以 渲染页面的时候渲染不上去
有用。
有用!!!!感谢分享!!!
有用。。。解决了buff转汉字的问题~谢谢