Javascript 通过Ajax将zip文件上载到Node.js
如何使用ajax将zip文件加载到服务器节点js而不使用表单,并将其解包 我试着这样做:Javascript 通过Ajax将zip文件上载到Node.js,javascript,ajax,node.js,forms,xmlhttprequest,Javascript,Ajax,Node.js,Forms,Xmlhttprequest,如何使用ajax将zip文件加载到服务器节点js而不使用表单,并将其解包 我试着这样做: function sendAjax(request) { xhr = new XMLHttpRequest(); xhr.open(request.method, request.url); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.
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);
});
及
但我犯了个错误
…server\node\u modules\adm zip\zipFile.js:66
抛出Utils.Errors.INVALID_格式;
^
无效或不受支持的zip格式。未找到结束标头
什么问题
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());