jk jk
jk jk

Reputation: 1075

Read blob as data url in JavaScript

I'm making a paste image from clipboard function then I need to convert blob data to base64 encoded data url, here's the code:

function handlepaste (event, e) { 
    var items = e.clipboardData.items;
    event.innerHTML = items[0].getAsFile();
    FileReader.readAsDataURL( event.innerHTML );

   if (event.childNodes && event.childNodes.length > 0) {
   $('body').append( event.innerHTML );
   }
    if (e.preventDefault) {
            e.stopPropagation();
            e.preventDefault();
    }
    return false;
}

but the code's not working for me, Chrome console log says:

Uncaught TypeError: Object function FileReader() { [native code] } has no method 'readAsDataURL'

How can I turn [object Blob] to data:image/png;base64,iVBORw...?

Upvotes: 2

Views: 5150

Answers (1)

bgusach
bgusach

Reputation: 15205

I think you haven't instantiated FileReader properly: var file_reader = new FileReader();

And then use file_reader.readAsDataURL(...)

Upvotes: 3

Related Questions