1、在 度娘搜索 “javascript 解壓縮gzip”,基本都是 pako.js相關的內容,先記錄下:
js Gzip前台壓縮字符串如何實現? - 知乎.html(https://www.zhihu.com/question/53169830)
Netty websocket 推送數據壓縮以 js解壓 - - ITeye博客.html(https://www.iteye.com/blog/zifeng412708-2320475)
JS GZIP壓縮,GZIP解壓_a_靖的博客-CSDN博客_js gzip.html(https://blog.csdn.net/qq_35713752/article/details/82079629)
2、遇到實際問題:ArrayBuffer(base64編碼的jpg圖片數據) 數據過長,導致 轉 javascript.string時報錯:
轉換的操作:String.fromCharCode.apply(null, new Uint8Array(data)); // 這里的 data就是 websocket傳來的數據(類型:ArrayBuffer) // ∵ 我Python傳來的是 信息是byte數組,∴這里使用Uint8Array強轉。其它情況可使用 Uint16Array等來強轉
報錯信息:String.fromCharCode.apply(null, new Uint8Array(data))
參考網址:(里面涉及到 一點點 pako的使用)
String.fromCharCode.apply Maximum call stack size exceeded – 前端開發,JQUERY特效,全棧開發,vue開發.html(https://www.jqhtml.com/62603.html)
網頁中代碼保存:
let strData = atob(result.data); // Convert binary string to character-number array let charData = strData.split('').map(function (x) { return x.charCodeAt(0); }); // Turn number array into byte-array let binData = new Uint8Array(charData); // // unzip let array = pako.inflate(binData); // ZC: pako 解壓 <-- <-- <-- // ZC: 很長的(8*1024字節)字符數字 轉 字符串 的方案 <-- <-- <-- <-- <-- strData = '' /** * String.fromCharCode.apply(null, array) 顯示 Maximum call stack size exceeded * 超過最大調用堆棧大小 */ let chunk = 8 * 1024; let i; for (i = 0; i < array.length / chunk; i++) { strData += String.fromCharCode.apply(null, array.slice(i * chunk, (i + 1) * chunk)); } strData += String.fromCharCode.apply(null, array.slice(i * chunk)); // 將亂碼的中文進行轉換 let jsonResult = decodeURIComponent(escape((strData)))
3、
4、
5、