如何将zip文件加载到没有表单的服务器节点js,只需使用ajax,然后解压缩。
我试图这样做:
function sendAjax(request) {
xhr = new XMLHttpRequest();
xhr.open(request.method, request.url);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200){
if (request.callback) {
request.callback(JSON.parse(xhr.response));
}
}
};
var data = null;
if (request.multipleData !== true) {
data = JSON.stringify(request.data);
} else {
var fd = new FormData();
fd.append('file.zip', request.data);
data = fd;
}
xhr.send(data);
}
服务器端的我正在做下一步:
var body = '';
request.on('data', function (data) {console.log(1111 + data);
body += data;
// 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
if (body.length > 1e8) {
// FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST
request.connection.destroy();
}
});
request.on('end', function () {console.log(222 + body);
router.route(url.parse(request.url, true), response, body);
});
和
var buf = new Buffer(file.length);
buf.write(file);
var zip = new AdmZip(buf);
var zipEntries = zip.getEntries(); // an array of ZipEntry records
for (var i = 0; i < zipEntries.length; i++) {
console.log(zip.file("content.txt").asText());
但是我收到了错误,
...服务器\ node_modules \ ADM-拉链\ zipFile.js:66 throw Utils.Errors.INVALID_FORMAT; ^ zip格式无效或不受支持。找不到END标题
出了什么问题?