5

I'm trying to use multer to process a profile photp upload on the frontend to the backend but keep getting this error:

{ [Error: Unexpected field]
code: 'LIMIT_UNEXPECTED_FILE',
field: 'file',
storageErrors: [] }

Below is my code:

register.ejs:

<div class="form-group">
    <input class="form-control" type="file" name="image" id="image">
</div>

ajax.js:

$('form').submit(function(e) {
        e.preventDefault();
        var formData = new FormData(this);
        // var img = cropper.getCroppedCanvas().toDataURL();
        // formData.append('img',img);
            $.ajax({
                url: '/register',
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function () {
                    console.log('Upload success');
                    },
                error: function () {
                    console.log('Upload error');
            }
        });
    });   

app.js:

var multer      = require('multer');
var cloudinary  = require('cloudinary');
...
// MULTER
var storage =   multer.diskStorage({
  destination: function(req, file, callback) {
    callback(null, "./public/uploads");
  },
  filename: function(req, file, callback) {
    callback(null, Date.now() + file.originalname);
  }
});

var upload = multer({ storage : storage}).single("image");

app.post("/register", function(req, res) {
  upload(req, res, function(err){
      if(err) {
           console.log(err);
           return res.send("Error uploading file.");
      }

     res.send("Upload success");
 });
});

I also get an undefined object when I try to get req.body.img or req.file. Maybe the formdata is not populated properly? Can anyone shed some light for me?

1
  • find a solution for this error? I am getting the same. Commented Jan 28, 2018 at 14:38

3 Answers 3

1

It says on multer docs that you should pass enctype="multipart/form-data" attribute to your <form> element. I'm not really familiar with ajax, but I think you may have to set contentType: in ajax.js to "multipart/form-data".

I also had a similar error, but it was caused by non-matching text-field keys.

Sign up to request clarification or add additional context in comments.

Comments

0

I have also face the same issue, instead of using

var upload = multer({ storage : storage}).single('image');

use the fields() method which allow to handle multiple files at once

app.use(multer({ storage : storage}).fields([{name:'image',maxCount:1}]));

file data store in req.files , then you can simply handle the files

that error occur when you try to handle multiple form data object in single multer middleware,

you can use the storage option to store different files in different locations.

further more can go thought official doc

Comments

0

When you call the multer function, maybe you forget to change the single field name. For example:

multer({ ... }).single("some_field_name");

Maybe some_field_name should be other_field_name. some_field_name and other_field_name are field names.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.