A
A
Adilet Kasymbaev2022-01-16 15:54:03
JavaScript
Adilet Kasymbaev, 2022-01-16 15:54:03

How to implement sending files through React to the backend?

Good afternoon. The problem is that I have two components: the first is an image drag and drop, all validated files are added to the state, the second is a regular form for creating a post. As I understand it, you can’t embed images in json, but I found a way to send data through FormData, however, I can’t figure out how to send both images (formdata) and the post data itself at the same time. I apologize for being too stupid, if suddenly the answer turns out to be too simple.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kirill Makarov, 2022-01-17
@Toasty

You send it through the format, new FormData, while the backender must know what the format is. And you will add all other parameters through formdata.append(name, param). Then there will be a full download of the file in the blob view.
Or in the base64 representation, but it will come out more expensive for itself.

B
bro-dev, 2022-01-19
@xPomaHx

At the moment of selecting a file, it is already loaded by a separate apiha somewhere on the back, and receives the file ID. Further, the form sends the usual json in which the fileID field contains the file ID of the previously received file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question