[javascript] Image convert to Base64

<input type="file" id="asd"/>

I would like to get the image in base64 once the user chose that (before submitting the form)

Something like :

  var data = $(this).val().base64file(); // it is not a plugin is just an example

I read about File API and other stuffs, I would like a simple and cross-browsers solution (IE6/IE7 excluded obviously)

Any help appreciated thanks.

This question is related to javascript jquery file input base64

The answer is

function readFile() {_x000D_
  if (this.files && this.files[0]) {_x000D_
    var FR= new FileReader();_x000D_
    FR.addEventListener("load", function(e) {_x000D_
      document.getElementById("img").src       = e.target.result;_x000D_
      document.getElementById("b64").innerHTML = e.target.result;_x000D_
    }); _x000D_
    FR.readAsDataURL( this.files[0] );_x000D_
document.getElementById("inp").addEventListener("change", readFile);
<input id="inp" type='file'>_x000D_
<p id="b64"></p>_x000D_
<img id="img" height="150">

(P.S: A base64 encoded image (String) 4/3 the size of the original image data)

Check this answer for multiple images upload.

Browser support: http://caniuse.com/#search=file%20api
More info here: https://developer.mozilla.org/en-US/docs/Web/API/FileReader

Similar questions with javascript tag:

Similar questions with jquery tag:

Similar questions with file tag:

Similar questions with input tag:

Similar questions with base64 tag: