用户数据发送到服务器前,如何用javascript压缩/gzip
How to zip/gzip user data in javascript before sending to the server?
我对Javascript还是个新手。我遇到过这样一种情况:许多用户可以向服务器发送大量JSON。为了限制流量,我想把它们压缩一下。这在Javascript中是可能的吗?如何从JSON的字符串表示创建字节数组?谢谢。
我知道没有gzip实现,但是有其他的压缩方法可供您使用。
这将使用JavaScript对字符串进行lzw编码:
// lzw-encode a string
function lzw_encode(s) {
var dict = {};
var data = (s + "").split("");
var out = [];
var currChar;
var phrase = data[0];
var code = 256;
for (var i=1; i<data.length; i++) {
currChar=data[i];
if (dict[phrase + currChar] != null) {
phrase += currChar;
}
else {
out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
dict[phrase + currChar] = code;
code++;
phrase=currChar;
}
}
out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
for (var i=0; i<out.length; i++) {
out[i] = String.fromCharCode(out[i]);
}
return out.join("");
}
我相信是的,这里有一篇关于这个主题的维基百科文章http://en.wikipedia.org/wiki/HTTP_compression
相关文章:
- 如何用javascript获取谷歌地图的经度和纬度
- 如何用javascript打印php数组
- 如何用javascript下载画布图像(base64)
- 如何用JavaScript或jQuery替换一堆标签
- 如何用javascript重写html文档
- 如何用javascript格式化日期
- 如何用javascript调用函数,然后在滚动事件中调用该函数的特定实例
- Javascript.如何用javascript将信息发送到另一个网站
- 如何用Javascript编写OR
- 如何用javascript从数组中填充无序列表
- 如何用javascript计算从现在开始的时间
- 如何用javascript替换Jquery字符串上的新参数
- 如何用javascript编写for循环
- 如何用javascript过滤JSON数据
- 我应该如何用javascript实例化这个日期
- 如何用javascript,phonegap(cordova)将json对象插入sqlite表中
- 如何用javascript下载巨大的CSV文件
- 如何用JavaScript替换HTML字符串
- 如何用JavaScript正则表达式替换href中的所有内容
- 如何用Javascript编写设备驱动程序