I\'m working on uploading a file to my app using the multer npm module.
The multer function I have defined is to allow a single file uploaded to the file system. Ev
since 2 images are getting uploaded! one with file extension and other file without extension. to delete tmp_path (file without extension)
after
src.pipe(dest);
add below code
fs.unlink(tmp_path); //deleting the tmp_path
Unfortunately, the error message doesn't provide clear information about what the real problem is. For that, some debugging is required.
From the stack trace, here's the origin of the error in the multer
package:
function wrappedFileFilter (req, file, cb) {
if ((filesLeft[file.fieldname] || 0) <= 0) {
return cb(makeError('LIMIT_UNEXPECTED_FILE', file.fieldname))
}
filesLeft[file.fieldname] -= 1
fileFilter(req, file, cb)
}
And the strange (possibly mistaken) translation applied here is the source of the message itself...
'LIMIT_UNEXPECTED_FILE': 'Unexpected field'
filesLeft
is an object that contains the name of the field your server is expecting, and file.fieldname
contains the name of the field provided by the client. The error is thrown when there is a mismatch between the field name provided by the client and the field name expected by the server.
The solution is to change the name on either the client or the server so that the two agree.
For example, when using fetch
on the client...
var theinput = document.getElementById('myfileinput')
var data = new FormData()
data.append('myfile',theinput.files[0])
fetch( "/upload", { method:"POST", body:data } )
And the server would have a route such as the following...
app.post('/upload', multer(multerConfig).single('myfile'),function(req, res){
res.sendStatus(200)
}
Notice that it is myfile
which is the common name (in this example).
probably you are not giving the same name as you mentioned in the
upload.single('file')
.
We have to make sure the type= file with name attribute should be same as the parameter name passed in
upload.single('attr')
var multer = require('multer');
var upload = multer({ dest: 'upload/'});
var fs = require('fs');
/** Permissible loading a single file,
the value of the attribute "name" in the form of "recfile". **/
var type = upload.single('recfile');
app.post('/upload', type, function (req,res) {
/** When using the "single"
data come in "req.file" regardless of the attribute "name". **/
var tmp_path = req.file.path;
/** The original name of the uploaded file
stored in the variable "originalname". **/
var target_path = 'uploads/' + req.file.originalname;
/** A better way to copy the uploaded file. **/
var src = fs.createReadStream(tmp_path);
var dest = fs.createWriteStream(target_path);
src.pipe(dest);
src.on('end', function() { res.render('complete'); });
src.on('error', function(err) { res.render('error'); });
});
In my case, I had 2 forms in differents views and differents router files. The first router used the name field with view one and its file name was "inputGroupFile02". The second view had another name for file input. For some reason Multer not allows you set differents name in different views, so I dicided to use same name for the file input in both views.
In my scenario this was happening because I renamed a parameter in swagger.yaml
but did not reload the docs page.
Hence I was trying the API with an unexpected input parameter.
Long story short, F5 is my friend.