简体   繁体   中英

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM