javascript 如何使用multer将文件从邮递员发送到node.js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31734164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I send a file from postman to node.js with multer
提问by mythicalcoder
Windows
Express 4.12.4
Multer 1.0.1
Node v0.10.22
Windows
Express 4.12.4
Multer 1.0.1
节点 v0.10.22
I'm trying to send a file to my node.js server using postman.
我正在尝试使用邮递员将文件发送到我的 node.js 服务器。
I'm attempting to follow the readme here
我正在尝试按照这里的自述文件进行操作
Here's what I'm sending with postman:
这是我与邮递员发送的内容:
POST /ingest HTTP/1.1
Host: localhost:3000
Cache-Control: no-cache
Postman-Token: 69dd2497-2002-56ed-30a4-d662f77dc0b0
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Here's what it's hitting on node.js
这是它在 node.js 上的表现
var Ingest = require('../controllers/ingest.js');
var multer = require('multer');
var upload = multer({ dest: 'uploads/',fileFilter:function(req,file,cb){
console.log('file is',file)
cb(null,true);
}
});
module.exports = function (app) {
app.post('/ingest', upload.single('test'), function(req, res, next) {
console.log(req.body);
console.log(req.file);
Ingest.ingestData()
.then(function (response){
return res.status(200).json(response);
});
});
}
When I hit this route with postman I get {}
for req.body
and undefined
for req.file
.
当我击中邮差这条路,我得到{}
了req.body
和undefined
为req.file
。
What am I doing wrong?
我究竟做错了什么?
Here's where I initialize the app
that gets passed in to the route file:
这是我初始化app
传入路由文件的地方:
var express = require('express');
var app = express();
var http = require('http');
var cfg = require('./config')();
var passport = require('passport');
var cors = require('cors');
var bodyParser = require('body-parser');
app.set('port', process.env.PORT || cfg.port);
var corsOptions = {
origin: "*",
allowedHeaders: ['Content-Type', 'Authorization', 'Accept', 'x-reset-token', 'x-invite-token', 'x-api-key', 'x-www-form-urlencoded'],
credentials: true
};
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(passport.initialize());
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Maybe something in there is doing it?
也许里面有什么东西在做?
Edit:
编辑:
I've even tried
我什至试过
var Ingest = require('../controllers/ingest.js');
var multer = require('multer');
var upload = multer({ dest: 'uploads/',fileFilter:function(req,file,cb){
console.log('file is',file)
cb(null,true);
}
}).single('csv');
module.exports = function (app) {
app.post('/ingest', function(req,res){
upload(req, res, function(err) {
if(err){
console.log(err);
}
console.log(req.body);
console.log(req.file);
Ingest.ingestData()
.then(function (response){
return res.status(200).json(response);
});
});
});
}
And that didn't help. It doesn't log anything for err
这没有帮助。它不记录任何内容err
回答by hassansin
In Postman screenshot, the file field name is missing. The key should be csv
since Multer accepts single file with that name.
在 Postman 屏幕截图中,缺少文件字段名称。关键应该是csv
因为 Multer 接受具有该名称的单个文件。
回答by taman neupane
回答by mythicalcoder
This would help others like me who are searching for a similar answer.
这将帮助像我这样正在寻找类似答案的其他人。
I used a curl request instead of Postman.
我使用 curl 请求而不是 Postman。
curl -v -F [email protected] http://localhost/url
There seems to be an issue with Postman.
Postman 似乎有问题。
https://github.com/expressjs/multer/issues/317
https://github.com/expressjs/multer/issues/317
I also have this issue when I use Postman to send file using form-data. The attribute req.file or req.files is always undefined. If anyone has used Postman and was able to retrieve the file using req.file or req.files, please comment.
当我使用 Postman 使用表单数据发送文件时,我也遇到了这个问题。属性 req.file 或 req.files 始终未定义。如果有人使用过 Postman 并且能够使用 req.file 或 req.files 检索文件,请发表评论。