简体   繁体   中英

How to dynamically concat a block of byte stream from http response in javascript?

So,

I want to send a stream of bytes to the browser(for example a pdf) so he can show it to the user.

if (bytesToRead == -1) {
                bytesToRead = (int)fullPathFile.length();}
            byte[] buffer = new byte[bytesToRead];
            int bytesRead = -1;     
            if((inputFileInputStream != null) && ((bytesRead = inputFileInputStream.read(buffer)) != -1)){ 

                if (codec.equals("base64")) {
                    String streamLength = Base64.encodeBytes(buffer, 0, bytesToRead);
                    response.setContentLength(streamLength.length());
                    outputFileOutputStream.write(Base64.encodeBytes(buffer, 0, bytesToRead).getBytes());

                } else {
                    outputFileOutputStream.write(buffer, 0, bytesToRead);
                }
            }
            inputFileInputStream.close();
            outputFileOutputStream.flush();
            outputFileOutputStream.close();

At the moment it works with option -1 to get the stream of byte in one go but I want to send 2kb lets says. I understand that for that I need to send multiple 2kb replies and I have to concatenate them in the javascript code. But, how do I do that ?

Loop through your arrays using concat()...

var allBytes = [];

for (byteArray in allByteArray){
    allBytes = allBytes.concat(byteArray);
}

But if you want to join multiple PDF pages it wont work...

我使用了for()来增加偏移量。

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