user16949958
user16949958

Reputation:

how to select file and convert to string in react?

So in my backend (express and mongodb) I have a field inside of post model called picture and it's type is string. So in the frontend (React) I want the user to be able to select a file and then the file will be converted to a string then sent to the backend. I do not know how to do this nor have I been able to find any tutorial or blog on this. Thanks in advance!

Upvotes: 0

Views: 4199

Answers (2)

Alexey Kureev
Alexey Kureev

Reputation: 2057

The functionality you are looking for is called FileReader API.

You might take a look at a very similar question here: How to convert file to base64 in JavaScript?.

Upvotes: 0

frankie303
frankie303

Reputation: 80

Google "convert file to string js". Then you need to do a POST request by putting that string to the BODY in your POST request but of course you need to handle this operation in your backend in one of your endpoints.

Upvotes: 0

Related Questions