Blob URL generation confusion

311 views Asked by At

I am using the following piece of code to generate blob URL for an image:

1) let arrayBufferView =  new Int8Array(this.response); 
    let blob = new Blob([arrayBufferView], {type : 'image/png'});
    let img = document.querySelector('img');
    img.src = URL.createObjectURL(blob);
    img.onload = function() {URL.revokeObjectURL(this.src);}

The JSON response is from a Java code which sends byte representation of image binary data. Byte in Java is 8-bit signed int. SO line 1 works fine But when I replace line 1 with let arrayBufferView = new Uint8Array(this.response); it still works fine. How will the 8bit signed from Java gets converted to Uint8Array (which is a 8 bit unsigned array view).

Then I tried the code with other variations of ArrayBuffer which are below:

let arrayBufferView =  new Int16Array(this.response);
let arrayBufferView =  new Uint16Array(this.response);
let arrayBufferView =  new Int32Array(this.response);
let arrayBufferView =  new Uint32Array(this.response);

For the above four ArrayBuffer the blob URL does not get generated and also why does it work for let arrayBufferView = new Uint8Array(this.response).

My understanding of blob URL is that byte representation sent from server is reconstructed by Blob constructor and the array buffer should correctly byte array sent from server. Then why does the Uint8Array work fine, but not Int16Array, Uint16Array, Int32Array and Uint32Array?

0

There are 0 answers