Javascript convert blob to string and vice versa

I can convert blob to string using FileReader, but I want to return it:

var reader = new window.FileReader(); reader.readAsDataURL(blob); reader.onloadend = function() { base64data = reader.result; var blobToSend = base64data.substr(base64data.indexOf(',')+1); rtcMultiConnection.send({"command":{ "recording":blobToSend, "type":blob.type, "size":blob.size }}); } 

This ships with https://github.com/muaz-khan/RTCMultiConnection , but the main question is how to restore the blob after sending. Unfortunately sending blob does not work either.

+5
source share
1 answer

source: Creating a Blob from a base64 string in JavaScript This method correctly converts base64 data back to its original binary data. To improve performance, data is processed in sliceSize blocks. NOTE: source is in TypeScript

  public static Base64ToBlob(b64Data, contentType = "", sliceSize = 512): Blob { const byteCharacters = atob(b64Data); const byteArrays = []; for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) { const slice = byteCharacters.slice(offset, offset + sliceSize); const byteNumbers = new Array(slice.length); for (let i = 0; i < slice.length; i++) { byteNumbers[i] = slice.charCodeAt(i); } const byteArray = new Uint8Array(byteNumbers); byteArrays.push(byteArray); } const blob = new Blob(byteArrays, { type: contentType }); return blob; } 
+2
source

Source: https://habr.com/ru/post/1246041/


All Articles