What is ByteArrayEntity and equivalent in JavaScript

I have tried reading through all of the documentation I can find on this and am still confused about what this actually is and how it will be received.

I am trying to recreate a connection api (originally written in java) in javascript. the Java is sending an image as a byteArrayEntity:

HttpPost postRequest = new HttpPost(requestUrl);
log("Request url: " + requestUrl);
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 30000);
postRequest.setParams(params);
HttpEntity entity = new ByteArrayEntity(photo);
postRequest.setEntity(entity);
Header header = new BasicHeader("Content-Type", "image/jpeg");
postRequest.addHeader(header);
header = new BasicHeader("Accept", "application/json");
postRequest.addHeader(header);

How can I do the same thing in javascript so that it will be recognised without having to perform any further operations on it?

Obviously I cannot do exactly the same thing in javascript, I can send it as a string of the bytearray, ie. "[42,34,24,...]" but assumably they would have to be expecting that on the other side and so know to convert this back into an array?

Thanks in advance.

2 Answers

Probably the byte array is base 64 encoded. You can prefix it like this on your server and the browser will see it as image data.

data:image/jpeg;base64,BBBBBB...

Where BBBBBB... is your byte array

Resolved: Thanks, eventually found that the only way this was recognised as an image was to send it as a Uint8Array within an arraybuffer. At the moment the receiving software is not matching this with a previous image so I'm unsure if they are expecting this in some other fashion, but the httprequest is recognised as a valid image.

var abufimg = new ArrayBuffer(array.length);
var vu8img = new Uint8Array(abufimg);
for(var y=0; y<array.length; y++){ vu8img[y] = array[y];
}
xhr.send(abufimg);

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like