后端给前端传压缩时,后端传字节类型,前端:
引入npm install pako;
import pako from “pako”;
axios:
export function getLg2TPlate(query) {
return request({
url: '/getLg2TPlate',
method: 'get',
params: query,
responseType: 'arraybuffer'(一定要加上,重点)
})
}
var binData = new Uint8Array(response);
this.formInline.match = undefined;
// 将数字数组转换为字节数组
var data = pako.inflate(binData);
// 转换gunzipped byteArray回ascii字符串:
var strData = '';
var chunk = 8 * 1024;
var i;
for (i = 0; i < data.length / chunk; i++) {
strData += String.fromCharCode.apply(null, data.slice(i * chunk, (i + 1) * chunk));
}
strData += String.fromCharCode.apply(null, data.slice(i * chunk));
this.chartdata = JSON.parse(strData);