如何从节点中的客户端提取zip [英] How to extract zip from client in node

查看:43
本文介绍了如何从节点中的客户端提取zip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个节点应用程序,需要从客户端邮递员获取一些zip文件并将其解压缩到我的文件系统中的文件夹中,我使用 express 以下不起作用,

我在这里想念什么?

我已经创建了示例节点应用程序来模拟问题.

  var express = require('express');var upload = require('multer')({dest:'uploads/'});var admZip = require('adm-zip');var app = express();app.post('/',upload.single('file'),function(req,res){调试器;var zip = new admZip(req.file);zip.extractAllTo("C://TestFolder//TestPathtoExtract",true);res.send("unzip");});var server = app.listen(3001,function(){var host = server.address().address;var port = server.address().port;console.log('示例应用程序在http://%s:%s上监听,主机,端口);}) 

这是我在邮递员中使用的方式

如果还有其他方法可以使用不同的开源,那就太好了!我用

现在在标题中,我尝试手动设置 multipart/form-data 并完全失败后留空了,所以这里没有标题.

在这里,我做了一对 console.log ,这是 req.headers 中的一个,以确保 Postman 发送正确的multipart/form-data 和另一个 req.file

输出似乎很好

代码.

  var express = require('express');var upload = require('multer')({dest:上传/"});var admZip = require('adm-zip');var app = express();app.post('/',upload.single('file'),function(req,res){console.log('%c> req.headers test.js [9]< =================================','color:blue;',req.headers);调试器;console.log('%c> req.file test.js [10]< =================================','color:blue;',req.file);//我使用req.file.path而不是req.file,因为admzip需要实际的文件路径var zip = new admZip(req.file.path);zip.extractAllTo("/Users/myuser/Desktop/ext",true);res.send("unzip");});var server = app.listen(3001,function(){var host = server.address().address;var port = server.address().port;console.log('示例应用程序在http://%s:%s上监听,主机,端口);}); 

I'm having a node app which needs to get some zip file from client Postman and extract it to a folder in my fileSystem,Im using express I did the following which doesnt work,

what am I missing here?

I've created sample node app to simulate the issue.

var express = require('express');
var upload = require('multer')({ dest: 'uploads/' });
var admZip = require('adm-zip');
var app = express();

app.post('/',upload.single('file'),function(req,res){
    debugger;
    var zip = new admZip(req.file);
    zip.extractAllTo("C://TestFolder//TestPathtoExtract", true);
    res.send("unzip");

});

var server = app.listen(3001,function(){
    var host = server.address().address;
    var port = server.address().port;
    console.log('Example app listening at http://%s:%s',host,port);
})

This is how I use it im postman

If there is other way to do it with different open source this can be great! I use https://github.com/cthackers/adm-zip

which can be change to any other library

I've also find this lib but not sure how to use it with express https://www.npmjs.com/package/decompress-zip

Thanks!

解决方案

This is the set up I did for Postman, first this is my form-data body

Now in the header I left in blank after trying to set multipart/form-data manually and utterly failed, so no header here.

Here I did a pair of console.log, one of req.headers to be sure of Postman sending the right multipart/form-data and another of req.file

And well the output seems to be fine

Edit: the code.

var express = require('express');
var upload = require('multer')({
  dest: 'uploads/'
});
var admZip = require('adm-zip');
var app = express();

app.post('/', upload.single('file'), function(req, res) {
  console.log('%c > req.headers test.js [9] <=================================', 'color:blue;', req.headers);
  debugger;
  console.log('%c > req.file test.js [10] <=================================', 'color:blue;', req.file);
  //instead of just req.file I use req.file.path as admzip needs the actual file path
  var zip = new admZip(req.file.path);
  zip.extractAllTo("/Users/myuser/Desktop/ext", true);
  res.send("unzip");

});

var server = app.listen(3001, function() {
  var host = server.address().address;
  var port = server.address().port;
  console.log('Example app listening at http://%s:%s', host, port);
});

这篇关于如何从节点中的客户端提取zip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆