0

I have an img in my project (/Content/Img/myPic.png) and i need to transform it into base 64 and save it in a string variable.

Something like:

let myFile = File("/Content/Img/myPic.png");
let fileBase64 = Base64Transformer(myFile);

I don't know how to access my file, and what to use to transform it. Any idea?

Lempkin
  • 1,458
  • 2
  • 26
  • 49
  • In modern browsers, check out https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/btoa – Carl Sep 27 '16 at 13:43
  • http://stackoverflow.com/questions/6150289/how-to-convert-image-into-base64-string-using-javascript#answer-20285053 seems to be what you are after – Carl Sep 27 '16 at 13:44
  • Ok but how can i access the file to give it to window.btoa() ? – Lempkin Sep 27 '16 at 13:44

1 Answers1

0

Try - angular-file-to-base64-directive, it takes a input file and preduce a base64.

Ouput example :

{
    "filesize": 54836, /* bytes */
    "filetype": "image/jpeg",
    "filename": "profile.jpg",
    "base64":   "/9j/4AAQSkZJRgABAgAAAQABAAD//gAEKgD/4gIctcwIQA..."
  }
Itsik Mauyhas
  • 3,824
  • 14
  • 69
  • 114