A
A
andrkkk2020-09-24 12:47:32
Node.js
andrkkk, 2020-09-24 12:47:32

How to send data with a file from the client (React) and receive it on nodejs?

There is a form, regular fields + file attachment
If you do this:

let data = {
    name,
    email,
    title,
    message,
    file: fileInput.current.files["0"],
}
let options = {
    method: "POST",
    headers: {
        "Content-Type": "application/json",
    },
    body: data,
}
fetch("http://localhost:8000/send", {
    options,
}).then(...

Not taking into account the file, the data comes correctly, bodyParser with JSON is installed on the server.
And how to transfer everything, along with the file? Next, I will save the file to the directory with multer.
Somewhere they write that you need to specify 'Content-Type': 'multipart/form-data' , somewhere that you need to remove the headers altogether. Doesn't come out at all

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Robur, 2020-09-24
@andrkkk

via FormData https://stackoverflow.com/a/61053359/1584465

K
Kirill Makarov, 2020-09-26
@kirbi1996

It is very convenient through the react-hook-form the minimum number of renders, well, as mentioned above, it will be necessary to append everything through the formdata

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question