1

I'm practicing in programming with websocket, i'm building a java websocket server and want to send a binary data to websocket client, then client side can get that data through websocket and save it to a local file on disk, but i don't know what way using javascript to write an arraybuffer into a file. My example code is shown below

var ws = new WebSocket(URL);
ws.binaryType = "arraybuffer";
ws.onmessage = function(evt) {
     if (evt.data instanceOf ArrayBuffer) {
         // binary message is riecieved from server and i want to save it as a local file 
     
     }
};

Thank you for any help

4
  • try to post with a code that you have tried and faced problems with that Commented Feb 23, 2020 at 6:10
  • check this post: stackoverflow.com/questions/24996443/… Commented Feb 23, 2020 at 6:12
  • Ok! i will give an example code for describing my problem Commented Feb 23, 2020 at 6:52
  • Did the answer below solve it for you? Commented Jan 8, 2021 at 14:39

1 Answer 1

1

You can create a textual/string representation that can be saved to and read from a file:

// returns String
function getArrayBufferString(arrayBuffer) {
  return new Uint8Array(arrayBuffer).toString()
}

// returns ArrayBuffer
function parseArrayBufferString(string) {
  return new Uint8Array(string.split(',')).buffer
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.