Coder1234
Coder1234

Reputation: 409

Why won't this react js file uploader work?

I am trying to send an image file to my backend express.js api from my react frontend. But in my console.log i just get an empty object that looks like {} on the backend side. What is wrong with react.js api call that wont let me see the file in the console.log on the backend.

//Backend API

app.post('/api/v1/media', function (req, res) {
  console.log(req.body);
  res.json('api called');
 });

//React JS api call

const files = e.target.files;
console.log(files);

    setLoading(true);
    const res = await fetch(
      'http://localhost:4000/api/v1/media',
      {
        method:"POST",
        headers: {'Content-Type':'application/json'},
        body: JSON.stringify(files[0])
      }`enter code here`
    )
    const file = await res.json();

Upvotes: 0

Views: 68

Answers (1)

Archies
Archies

Reputation: 86

Try using

"Content-type" : "multipart/form-data"

Also Refer to this post for file uploading reference.

Upvotes: 1

Related Questions