How to send file from mailbox to node.js using multer

Window
Express 4.12.4
Multer 1.0.1
Node v0.10.22

I am trying to send a file to my node.js server using a postman.

I am trying to execute readme here

Here is what I am sending with the 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 looks like: enter image description here

Here is what it clicks on 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 find this route with the postman, I get {} for req.body and undefined for req.file .

What am I doing wrong?

Here, where I initialize the app , which is passed to the route file:

 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 is doing there?

Edit:

I 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 it didn’t help. He writes nothing for err

+4
source share
3 answers

Postman screenshot does not have a file name. The key must be csv , since Multer accepts a single file with this name.

+3
source

This will help others like me who are looking for a similar answer.

I used a curl request instead of Postman.

 curl -v -F csv=@file.csv http://localhost/url 

The problem seems to be related to Postman.

https://github.com/expressjs/multer/issues/317

I also have this problem when I use Postman to submit a file using form data. The req.file or req.files attribute is always undefined. If someone used Postman and was able to get the file using req.file or req.files, please comment.

+6
source

Just remove the Content-Type: application / x-www-form-urlencoded header from the postman.

enter image description here

-2
source

All Articles