小程序stream乱码问题处理

一 转载

https://developers.weixin.qq.com/community/develop/article/doc/000c22df268c581038f23929f61413

二 代码

requestTask.onChunkReceived(res => {
  // res 流式数据  注意:这里可能是多块数据,服务推送多次信息,onChunkReceived只响应一次,则该次监听的内容就是服务器推送多次拼接在一起的字符串,需要单独裁剪额外处理
  try {
    // 解码分块数据
    const uint8Array = new Uint8Array(res.data);
   let test = String.fromCharCode.apply(null, uint8Array);
   test = decodeURIComponent(escape(test));
   let testArr = test.split('data:');
    console.log(testArr, '====9999===');  // 这里就是服务器推送的原始内容
    // 后续用户自己的操作。。。
  }
  catch(err){
   console.error('推送数据结构异常!', err); 
 }
})

 

posted @ 2025-12-12 22:22  qx和世界  阅读(1)  评论(0)    收藏  举报