Convert blob to base64

后端 未结 9 2009
无人共我
无人共我 2020-11-22 13:12

This is a snippet for the code that I want to do Blob to Base64 string:

This commented part works and that when the URL generated by this i

相关标签:
9条回答
  • 2020-11-22 13:25

    you can fix problem by:

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

    I hope this help you

    0 讨论(0)
  • 2020-11-22 13:30

    I wanted something where I have access to base64 value to store into a list and for me adding event listener worked. You just need the FileReader which will read the image blob and return the base64 in the result.

    createImageFromBlob(image: Blob) {
        const reader = new FileReader();
        const supportedImages = []; // you can also refer to some global variable
        reader.addEventListener(
          'load',
          () => {
            // reader.result will have the required base64 image
            const base64data = reader.result;
            supportedImages.push(base64data); // this can be a reference to global variable and store the value into that global list so as to use it in the other part
          },
          false
        );
        // The readAsDataURL method is used to read the contents of the specified Blob or File.
        if (image) {
          reader.readAsDataURL(image);
        }
     }
    

    Final part is the readAsDataURL which is very important is being used to read the content of the specified Blob

    0 讨论(0)
  • 2020-11-22 13:33

    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);
    };
    
    0 讨论(0)
  • 2020-11-22 13:35

    Most easiest way in a single line of code

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

    0 讨论(0)
  • 2020-11-22 13:37
    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);
    }
    
    0 讨论(0)
  • 2020-11-22 13:39

    There is a pure JavaSript way that is not depended on any stacks:

    const blobToBase64 = blob => {
      const reader = new FileReader();
      reader.readAsDataURL(blob);
      return new Promise(resolve => {
        reader.onloadend = () => {
          resolve(reader.result);
        };
      });
    };
    

    For using this helper function you should set a callback, example:

    blobToBase64(blobData).then(res => {
      // do what you wanna do
      console.log(res); // res is base64 now
    });
    

    I write this helper function for my problem on React Native project, I wanted to download an image and then store it as a cached image:

    fetch(imageAddressAsStringValue)
      .then(res => res.blob())
      .then(blobToBase64)
      .then(finalResult => { 
        storeOnMyLocalDatabase(finalResult);
      });
    
    0 讨论(0)
提交回复
热议问题