简体   繁体   中英

Multer is giving me undefined files

I have this nodejs application. Very simple.

var express = require('express');
var multer = require('multer');

var upload = multer({ dest: 'c:\\temp\\crnode\\' });

var app = express();

app.set('port', (process.argv[2] || 64312));

// alive check
app.get('/youalive', function(req, res) {
    res.send('yup');
});

app.post('/cruseritems/put', upload.single('useritem'), function (req, res, next) {
    console.log(req.file);
    console.log(req.files);
    res.status(204).end();
});

var port = app.get('port');
app.listen(port, function() {
    console.log('Listening to ' + port + '...');
});

I borrowed this code from the github site itself ( here ). It does not work. No matter how I configure the client request, req.file , req.body , and req.files are always undefined. What am I doing wrong?

I am using PostMan to generate requests. Here is the last request I tried:

POST /cruseritems/put HTTP/1.1
Host: thm02426:64312
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Cache-Control: no-cache
Postman-Token: b9afba7c-3c5d-9144-13d3-5ea1b4a203a4

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="useritem"; filename="dhtmled0_.pdf"
Content-Type: application/pdf


----WebKitFormBoundary7MA4YWxkTrZu0gW 

I found the problem. Do not specify a content type manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM