在客户端(用Electron和TypeScript做的)压缩文件传给服务器

1, 先安装pako包 

npm install pako

2, 导入pako包

import pako from "pako";

3, 进行文件gzip压缩


export function Compress(data):ArrayBuffer{
	// 数据压缩
	const compressedData = pako.gzip(JSON.stringify(data));

	return toArrayBuffer(compressedData);

}

function toArrayBuffer(byteArray: Uint8Array): ArrayBuffer {
	const arrayBuffer = new ArrayBuffer(byteArray.length);
	new Uint8Array(arrayBuffer).set(byteArray);
	return arrayBuffer;
}

let strdata = Compress(jsonStr);

var blob = new Blob([strdata]);

let formData = new FormData()
formData.append('a', blob)

你可能感兴趣的:(Electron,electron,typescript,javascript,前端)