2

I am new to Node JS want to create Rest API for Upload Image and facing this issue.

I want to create a post method with multiple files and additional data from inputs. This is my code:

index.js :

app.post('/upload-photos', upload.array('photos'), function (req, res) {
  const uploadInfo = req.files.map(file => {
    return {
      sourceName: file.originalname,
      newName: file.filename
    };
  });
  res.send(uploadInfo);
});

My issue is I want to add some form data like (name, address, phone), has anyone else experienced this, please help me.

Thanks.

1 Answer 1

2

When using multer additional fields can be accessed through req.body.

app.post('/upload-photos', upload.array('photos'), function (req, res) {
  const { name, address, phone } = req.body;

  const uploadInfo = req.files.map(file => {
    return {
      sourceName: file.originalname,
      newName: file.filename
    };
  });
  res.send(uploadInfo);
});

In your form:

<form action="/upload-photos" method="post" enctype="multipart/form-data">
  <input type="file" name="photos" multiple />
  <input type="text" name="name" />
  <input type="text" name="address" />
  <input type="text" name="phone" />
</form>
Sign up to request clarification or add additional context in comments.

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.