multer: Cant get the middleware work in express 4
I am just following the readme tutorial and encountered the problem of
Error: Unexpected field
at makeError (*\node_modules\multer\lib\make-error.js:12:13)
my code look like this:
var express = require('express');
var app = express();
var multer = require('multer');
var upload = multer({ dest: './uploads/'});
app.post('/image',upload.single('avatar'), function (req, res) {
var image = req.body;
res.status(204).end()
});
my client is angular from the tutorial :https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs
I cant find the solution can you help me?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 1
- Comments: 17 (7 by maintainers)
What is the field name of the file uploaded?
<input type="file" name="THISVALUE" />
The code you are using is expecting it to be
avatar
.Got it. I was using ng2-file-upload to send the XHR request. By default they set the name of all the fields to
file
.@dmreinoso so in your code there it says
.single('file')
meaning one file allowed under the field namefile
…your html should have a line to
<input type="file" name="file"
if thename="file"
is notfile
that is your issue, otherwise you might have 2<input type="file"
and with.single()
you are telling it only 1 should exist in which case you should use.array("file",<count>)
…if all of the above conditions are satisfied then there is an issue let us know! 😃
I am getting the same LIMIT_UNEXPECTED_FILE error… But the condition is lil’ weird… I am using angular as frontend… I made a form and upload the file… it works… Form/page isn’t refreshed… use the same form…change the file and submit again… error… And the problem is… when i refresh the page/form… it works again… I checked the data sent too… Its single file… What could be the possible reason for this?
You are rigth, I made a simple web form with one “file” input and it worked.
It appears that “Advanced Rest Client” a Chorme extension is sending more than one “file” field.