Rectangle 27 0

javascript image files corrupt with websocket file transfer?


ws.binaryType = "blob";
ws.onmessage = function (event) {
    if (event.data instanceof Blob) {
        // event.data is a Blob
    } else {
        // event.data is a string
    }
}
ws.onmessage = function (event) {
    raw = window.atob(event.data);
}

@rajuk, the web workers specification requires base64 support. If it's missing then that's a bug and you should file a webkit bug: webkit.org/quality/reporting.html

@rajuk: I added an update inline referring to websockify which contains a very fast pure Javascript base64 encode/decode implementation.

For example you could base64 encode the data at the server and then base64 decode the data in the client:

If that is not an option and you can only send text frames from the server, then you will need to encode the binary data to text before sending it from the server and then decode the text on the other end. If you try and send binary data directly as text frames over WebSockets then doing charCodeAt(x) && 0xff will result in corrupt data.

If you have control of the WebSocket server then the best thing would be to send the data as binary frames instead of UTF-8 text data. If you can get the server to send the data as binary frames then you can just tell the WebSocket to deliver the data as Blobs:

It looks like you are reading data from a WebSocket as a string, converting it to a Blob, and then writing this to a file.

There is a very well performing pure Javascript base64 decode/encode contained in websockify. It decodes to an an array of numbers from 0-255 but could be easily modified to return a string instead if that is what you require (Disclaimer: I made websockify).

sorry!,Here I have a problem I am doing it in worker so no btoa atob functions allowed. anyway thanks for your response

Note