Skip to content
Fix Code Error

How can I convert an image into Base64 string using JavaScript?

March 13, 2021 by Code Error
Posted By: Anonymous

I need to convert my image to a Base64 string so that I can send my image to a server.

Is there any JavaScript file for this? Else, how can I convert it?

Solution

You can use the HTML5 <canvas> for it:

Create a canvas, load your image into it and then use toDataURL() to get the Base64 representation (actually, it’s a data: URL, but it contains the Base64-encoded image).

Answered By: Anonymous

Related Articles

  • Canvas position in grid inaccurate
  • My DIV elements go diagonal instead of horizontal
  • Tkinter understanding mainloop
  • How to save an HTML5 Canvas as an image on a server?
  • HTML5 Canvas Resize (Downscale) Image High Quality?
  • How to Copy Contents of One Canvas to Another Canvas Locally
  • Use HTML5 to resize an image before upload
  • How to create a game over screen for a basic HTML/JS game?
  • functions deleting the paint canvas
  • canvas.toDataURL() SecurityError

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How do I sort a list of dictionaries by a value of the dictionary?

Next Post:

How to Right-align flex item?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error