Javascript将blob转换为字符串并返回

Javascript将blob转换为字符串并返回,javascript,filereader,Javascript,Filereader,我可以使用FileReader将blob转换为字符串,但我想将其转换回: var reader = new window.FileReader(); reader.readAsDataURL(blob); reader.onloadend = function() { base64data = reader.result; var blobToSend = base64data.substr(base64data.indexOf(',')+1); rtcMultiConnect

我可以使用FileReader将blob转换为字符串,但我想将其转换回:

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
   }});
}
这是与一起发送的,但主要问题是如何在发送后重建blob。遗憾的是,按原样发送blob无效。

来源: 此方法正确地将base64数据转换回原始二进制数据。 为了提高性能,数据以sliceSize大小的块进行处理。 注意:源代码在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;
    }
publicstaticbase64toblob(b64Data,contentType=“”,sliceSize=512):Blob
{
常量ByTechCharacters=atob(B64数据);
常量字节数组=[];
for(让offset=0;offset
Chrome支持阵列缓冲区,RTCMulticonnection也支持阵列缓冲区。chrome中的Blob支持正在进行中。现在,您可以使用“fileReader.readAsArrayBuffer”。仅供参考:
connection.send(recorder.blob)
RTCMultiConnection将自动共享整个blob(任意大小)。远程用户将在“onfleend”事件中收到完整的blob。