Binary to Base64 Conversion
Why convert into Base64 format?
The advantages of Base64 encode are available to transmit data from binary, into (most commonly) ASCII characters. Due to the likeliness that the receiving end can handle ASCII, it makes it a nice way to transfer binary data, via a text stream
Why did I convert binary data into Base64?
The JSON format natively doesn’t support binary data. Binary files cannot be sent in JSON format and needed the whole post data to be sent in form-data format. I had very deeply nested data and it was difficult to manage it as form data hence I opted for base64 and sent the data as a base64 encoding.
How to convert?
- Add input field with type file. Call a function on click event lets say handleImage()
- In handleImage function:
- If you want to validate the image file by size or file extension and many custom validations, then add the validateImage function before conversion
- To check if the image is base64 or not: S.match(“;base64”).
Check the full code here. https://github.com/shrestharohit/binary-to-base-64-conversion