0

I got following Code in jQuery:

$.get("img/background.jpg", function(img){
    // Convert img to base64
    var base64Img = $.base64.encode(img);
});

The Plugin I am using in the example is https://github.com/carlo/jquery-base64 - which is not working at all.

Is there any way to get the Base64 (string) after getting an image in jQuery?

Thank you very much.

Tream
  • 1,049
  • 2
  • 13
  • 29
  • You don't need to send ajax request to get an image. In javascript you can get image by simply creating new Image() object and setting the src property to the url of the image resource – Dejan Bogatinovski Apr 01 '15 at 12:29
  • There is already answer about this question here on stackoverflow http://stackoverflow.com/questions/6150289/how-to-convert-image-into-base64-string-using-javascript – Dejan Bogatinovski Apr 01 '15 at 12:53
  • var base64Img = $.base64.encode(window.btoa(encodeURIComponent(img))); – websky Apr 01 '15 at 13:37
  • **Error:** INVALID_CHARACTER_ERR: DOM Exception 5 https://github.com/carlo/jquery-base64/issues/1 – websky Apr 01 '15 at 13:38
  • – websky Apr 01 '15 at 13:39
  • And ... 1. script must be run on the server 2. add file jquery.base64.js – websky Apr 01 '15 at 13:39
  • solution: `var base64Img = $.base64.encode(window.btoa(encodeURIComponent(img)));` – websky Apr 01 '15 at 13:39

0 Answers0