最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

filereader - Javascript convert blob to string and back - Stack Overflow

programmeradmin1浏览0评论

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

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

This is sent with but the main question is how to reconstruct the blob after being sent. Sadly sending the blob as is didn't work.

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

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

This is sent with https://github./muaz-khan/RTCMultiConnection but the main question is how to reconstruct the blob after being sent. Sadly sending the blob as is didn't work.

Share Improve this question edited Mar 30, 2016 at 6:44 Pavlo 45k14 gold badges83 silver badges114 bronze badges asked Mar 30, 2016 at 6:30 msj121msj121 2,8424 gold badges30 silver badges58 bronze badges 1
  • 1 Chrome supports array-buffer, and RTCMultiConection support it as well. Blob-support in chrome is work-in-progress. So for now, you can use "fileReader.readAsArrayBuffer". For your information, this will work: connection.send(recorder.blob) RTCMultiConnection will auto share the entire blob (of any size). Remote users will receive the plete blob in "onFileEnd" event. – Muaz Khan Commented Mar 30, 2016 at 7:17
Add a ment  | 

1 Answer 1

Reset to default 4

source: Creating a Blob from a base64 string in JavaScript This method correctly converts back base64 data to the original binary data. For the sake of performance improvements, the data is processed in blocks of the size of sliceSize. 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;
    }
发布评论

评论列表(0)

  1. 暂无评论