Blob object to base64 in JavaScript Blob object to base64 in JavaScript google-chrome google-chrome

Blob object to base64 in JavaScript


Nick Retallack's answer at this page How does the paste image from clipboard functionality work in Gmail and Google Chrome 12+? does exactly what I want.

So the new piece of code is :

var items = e.clipboardData.items;if (items) {// Loop through all items, looking for any kind of image    for (var i = 0; i < items.length; i++) {        if (items[i].type.indexOf("image") !== -1) {            // We need to represent the image as a file,            var blob = items[i].getAsFile();            var reader = new FileReader();                reader.onload = function(event){                    createImage(event.target.result); //event.target.results contains the base64 code to create the image.                };                reader.readAsDataURL(blob);//Convert the blob from clipboard to base64            }        }    }