Fetch流式获取数据

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;
      }
    },

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值