Convert blob to base64

This is the code snippet that I want to make Blob before Base64 :

This part of the comment works, and when the URL generated with this parameter is set to img src, it displays the image:

 var blob = items[i].getAsFile(); //var URLObj = window.URL || window.webkitURL; //var source = URLObj.createObjectURL(blob); //console.log("image source=" + source); var reader = new FileReader(); reader.onload = function(event){ console.log(event.target.result) }; // data url! var source = reader.readAsBinaryString(blob); 

The problem with the lower code, the generated source variable is null

Update:

Is there an easier way to do this using jQuery to be able to create a Base64 String from a blob file, as in the above code?

+91
javascript jquery
Sep 06 '13 at 4:55 on
source share
7 answers
  var reader = new FileReader(); reader.readAsDataURL(blob); reader.onloadend = function() { base64data = reader.result; console.log(base64data); } 

Generate documents readAsDataURL encodes in base64

+189
Sep 06 '13 at 5:05 on
source share

this worked for me:

 var blobToBase64 = function(blob, callback) { var reader = new FileReader(); reader.onload = function() { var dataUrl = reader.result; var base64 = dataUrl.split(',')[1]; callback(base64); }; reader.readAsDataURL(blob); }; 
+20
Oct 27 '16 at 16:19
source share

So the problem is that you want to load the base image 64, and you have the URL of the blob. Now an answer that will work in all HTML 5: Do browsers:

  var fileInput = document.getElementById('myFileInputTag'); var preview = document.getElementById('myImgTag'); fileInput.addEventListener('change', function (e) { var url = URL.createObjectURL(e.target.files[0]); preview.setAttribute('src', url); }); function Upload() { // preview can be image object or image element var myCanvas = document.getElementById('MyCanvas'); var ctx = myCanvas.getContext('2d'); ctx.drawImage(preview, 0,0); var base64Str = myCanvas.toDataURL(); $.ajax({ url: '/PathToServer', method: 'POST', data: { imageString: base64Str }, success: function(data) { if(data && data.Success) {}}, error: function(a,b,c){alert(c);} }); } 
+4
04 Feb '17 at 8:50
source share

you can fix the problem:

 var canvas = $('#canvas'); var b64Text = canvas.toDataURL(); b64Text = b64Text.replace('data:image/png;base64,',''); var base64Data = b64Text; 

I hope this helps you

+3
Dec 15 '16 at 12:36
source share
 var audioURL = window.URL.createObjectURL(blob); audio.src = audioURL; var reader = new window.FileReader(); reader.readAsDataURL(blob); reader.onloadend = function () { base64data = reader.result; console.log(base64data); } 
+3
Feb 02 '17 at 15:16
source share

The easiest way in one line of code

var base64Image = new buffer (blob, 'binary'). toString ('base64');

+1
Apr 04 '18 at 13:06 on
source share
 (async () => console.log(btoa(await new Response(blob).text())))(); 

or

 new Response(blob).text().then(t => console.log(btoa(t))) 

see Constructor of responses , you can turn [blob, buffer source form data, readable stream, etc.] into Response , which can then be turned into [json, text, array buffer, blob] which performs the asynchronous method / callbacks.

0
May 20 '19 at 9:30
source share



All Articles