TextDecoder()是JavaScript的一个内置对象,它用于将字节数组(如ArrayBuffer或TypedArray)解码为字符串。它可以处理各种字符编码,例如UTF-8、UTF-16等。
const byteArray = new Uint8Array([72, 101, 108, 108, 111]); // 字节数组
const decodedString = decoder.decode(byteArray);
console.log(decodedString); // "Hello"
async send() {
this.$refs["code"].innerText = "";
// 创建解码对象
let td = new TextDecoder();
let ai = await fetch(
"https://api.binjie.fun/api/generateStream?refer__1360=n4+xgDBD9DyDuDmxWweiqGNDQH4dLmieuz4PD",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
network: true,
prompt: this.textarea,
stream: false,
system: "",
userId: "#/chat/1706583670768",
withoutContext: false,
}),
}
);
// 获取读取可读流
let reader = ai.body.getReader();
while (1) {
let res = await reader.read();
let str = td.decode(res.value);
if (res.done) {
break;
}
this.$refs["code"].innerText += str;
}
},
Fetch流式获取数据
于 2024-01-30 14:21:18 首次发布