Convert blob to base64

后端 未结 9 1994
无人共我
无人共我 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: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

提交回复
热议问题