-1

Need to get Base64 from static img html element.

Code

<img id="imgDownload" src="path to image"/>
var data = btoa(document.getElementById("imgDownload"))

But btoa is showing compile error. Have send the base64 encoded code to server to download the image. Thanks in Advance

anand
  • 1,559
  • 5
  • 21
  • 45
  • See this https://stackoverflow.com/questions/6150289/how-to-convert-image-into-base64-string-using-javascript – Sam Feb 27 '18 at 11:15
  • 1
    Possible duplicate of [How to convert image into base64 string using javascript](https://stackoverflow.com/questions/6150289/how-to-convert-image-into-base64-string-using-javascript) – Sam Feb 27 '18 at 11:16
  • how to load image on canvas??? – anand Feb 27 '18 at 11:31
  • in IE on executing canvas.toDataURL("image/png"); it throwing error as "SecurityError" – anand Feb 27 '18 at 11:35

1 Answers1

0

The value of data in

var data = btoa(document.getElementById("imgDownload"))

is

W29iamVjdCBIVE1MSW1hZ2VFbGVtZW50XQ==

which when you decode it is: [object HTMLImageElement].

That's not what you want to send, I assume.

You might want: var data = btoa(document.getElementById("imgDownload").src) if you're after the URL.

If you're after the actual image bits, then you'll need to download the image - as someone else commented, that can be found here: How to convert image into base64 string using javascript

Pseudonymous
  • 839
  • 5
  • 13