Javascript How to create and use blobs with pure binary data

I have a pure Uint8Array which I want to store in blob I can create blolb with any type ('text / plain', application / octet-stream, etc.).

var a_blob = new Blob(Uint8Array,{type: whatever});

      

Later, to check the results I have:

this.read_blob = function (blob)  {
var reader = new FileReader();        
reader.onloadend =  function (evt) 
{ var full_buffer = evt.target.result;    }
reader.readAsArrayBuffer(blob);    
}

      

I saved 1 219 2 0 values ​​(4 values ​​using Uint32Array) and I did everything right.

Now I have: (I want to check blob values)

var data  = new Uint32Array (full_buffer,0,4);

      

And I have: 0: 808464433 1: 809054514 2: 808464432 3: 808464432 ????????????

If I use:

var data  = new Uint8Array (full_buffer,0,4);

      

I have: [49,48,48,48,50,49,57,48,48, ...] which are ascii codes for 1 0 0 0 2 1 9 0 .... (string representation of my values ....)

What am I doing badly? Maybe the blob is really not stored as pure binary data? Is fullbuffer a binary string object? Am I using a specific type? Should I read the buffer differently? Do I need to create a Uint8Array using the aspient full_buffer for individual data in a loop?

+3


source to share


1 answer


solved

As MDN explains: To create a blob, you must use

blob = new Blob (part, type); part = An Array of data objects to place in the new Blob.

So the fix (not easy to spot because you yourself see one and more temporary arrays, bufferarray, typedarrays .... and you feel confused) is to use [] brackets to create an array of / s elements on the fly.

So instead of:



var a_blob = new Blob(Uint8Array,{type: whatever});

      

using

var a_blob = new Blob([Uint8Array],{type: whatever});

      

Trust me, after doing a lot of internet searching, you can see posts where you can see the blob being created with and without parentheses

0


source







All Articles