Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :

$(input).on('change',function(){   var data = $(this).val().base64file(); // it is not a plugin is just an example   alert(data); }); 

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.

like image 754
bombastic Avatar asked Jul 17 '13 21:07

bombastic


People also ask

Can we convert image to Base64?

Convert Images to Base64Just select your JPG, PNG, GIF, Webp, or BMP picture or drag & drop it in the form below, press the Convert to Base64 button, and you'll get a base-64 string of the image. Press a button – get base64. No ads, nonsense, or garbage. Drag and drop your image here!

What is image to Base64?

Base64 encoding is a way to encode binary data in ASCII text. It's primarily used to store or transfer images, audio files, and other media online. It is also often used when there are limitations on the characters that can be used in a filename for various reasons.


1 Answers

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

like image 186
Roko C. Buljan Avatar answered Oct 05 '22 19:10

Roko C. Buljan