0

I am trying to send data to express from react. I have array of images which I have to send, but since I cannot send a single image as a test I will have to clearify what is happening. I set hardcoded image just for testing, since I have tried almost everything without success I want to do the simplest upload possible and I get null in the server side.

React

    const formData = new FormData();

    formData.append('files', cartImg);

    axios
      .post(
        `http://localhost:8800/imgUpload`,
        { formData },
        {
          headers: { 'Content-Type': 'multipart/form-data' },
        }
      )
      .then((res) => setMessage(res.data));

Express

app.post('/imgUpload', (req, res) => {
  console.log(req.files);
});

1 Answers1

0

Please add this in your server configuration

const express = require('express');
const app = express();

app.use(express.urlencoded({
    extended: true
}));

Check now you will be able to receive the form data in req.body

e.g req.body.files

app.post('/imgUpload', (req, res) => {
  console.log(req.body.files);
});
  • I have data:image/png;base64,iVBO,... in req.body.files. Now I want to add that image in a folder. And I get this error: req.body.files.mv is not a function – Predrag May 24 '23 at 10:17
  • refer this https://stackoverflow.com/questions/6926016/how-can-i-save-a-base64-encoded-image-to-disk, it may helps you – MAREESKANNNAN RAJENDRAN May 24 '23 at 10:30