Monday, 9 December 2019

How to convert text to ArrayBuffer like responseType of ArrayBuffer in Javascript

I am using a library to make an HTTP call, I know it is using XMLHTTPRequest, but I can't set responseType and response is always text.

I need to convert the text to ArrayBuffer as I used responseType: ArrayBuffer. I tried multiple conversion but none of them produce the same buffer.

Update: So the code below is what converts the ArrayBuffer to the string.

    var dataView = new DataView(this.response);
    var decoder = new TextDecoder('utf8');
    var decodedString = decoder.decode(dataView);

I need to reverse this

   var encodedr = new TextEncoder('utf8');              
   var encodedArray =encodedr.encode(req.body);
   var arrayBuffer = encodedArray.buffer;

is not the same and is almost twice the size.

Update 2: Glitch with code example https://successful-pepper.glitch.me/



from How to convert text to ArrayBuffer like responseType of ArrayBuffer in Javascript

No comments:

Post a Comment