0

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!

1

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

4 Comments

But how will I even open a file picker?
When i search convert file to string js it tells me to use JSON.stringify(myObj) but what do I replace the myObj with? Please explain. Because if I replace it with inputFile.current.files[0] it just gives me an empty string
This answer has no relevance to the original question.
0

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?.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.