A
A
Alexey Kostyukhin2021-11-17 11:05:11
Node.js
Alexey Kostyukhin, 2021-11-17 11:05:11

How can nodejs read the data that came from the client using formdat?

How to read the data correctly, and most importantly, from where in nodejs (Express framework) that come from the client?
if this data is some json, then everything is simple. All that I need is on req.body, but what if I use formdata and put, for example, a picture in binary format?

Here is the client code (react)

async function onSubmit() {
    const fileFormData = new FormData();
    fileFormData.append('file', file, file.name);
    fileFormData.append('name', 'TEST_NAME');

    const result = await Execute('file/upload', {
      method: 'POST',
      body: fileFormData,
      headers: {
        'CRU-Authorization': localStorage.getItem('login') || '',
        'auth_token': localStorage.getItem('auth_token') || '',
      }
    });
    console.log(result);
  }


Here is the backend code
router.post("/upload", async function(request, response) {
console.log(request.body)
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lssssssssssl, 2021-11-17
@aleksey4uk

https://metanit.com/web/nodejs/10.1.php
https://github.com/expressjs/multer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question