display and save a local image into database in react js

Viewed 2246

I'am developing a blog website using react.js where users can post and comment ...
so we know that a user can comment with an image ,and let's say he want to upload a local image that stored in his machine,i want to display that image as a comment and save it into postgres database which i'am using for that.
My question is what are the steps that i should follow to achieve my purpose,thanks in advance.

1 Answers

If you have to do in simple steps, you can save the image to base64, its just string representation of an image. So it can be saved in DB directly and when you have to show it just decode the string and display it accordingly.
References :-

  1. https://github.com/dankogai/js-base64#readme
  2. https://scotch.io/tutorials/how-to-encode-and-decode-strings-with-base64-in-javascript

Alternatively, what you can do when user selects an image, then upload that image to your server and get the path to that(multipart file upload) and from path you can show it where you want to show.
References :-

how to send a multipart/form-data from React.js with an image?

I hope it helps, Thanks :)

Related