Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8281267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:58:16+00:00 2026-06-08T09:58:16+00:00

I am using websockets for file transfer, while i am downloading a file i

  • 0

I am using websockets for file transfer, while i am downloading a file i am recieving the data as it is, but when I open an image file it was corrupted. Data files are downloading fine, the code goes as follows.

try {
    fileEntry = fs.root.getFile(filename, { create : creat_file });
    var byteArray = new Uint8Array(data.data.length);
    for (var i = 0; i < data.data.length; i++) {
        byteArray[i] = data.data.charCodeAt(i) & 0xff;
    }
    BlobBuilderObj = new WebKitBlobBuilder();
    BlobBuilderObj.append(byteArray.buffer);
    if (!writer) {
        writer = fileEntry.createWriter();
        pos = 0;
    }
    //self.postMessage(writer.position);
    writer.seek(pos);
    writer.write(BlobBuilderObj.getBlob());
    pos += 4096;
    }
catch (e) {
    errorHandler(e);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:58:18+00:00Added an answer on June 8, 2026 at 9:58 am

    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.

    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:

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

    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.

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

    ws.onmessage = function (event) {
        raw = window.atob(event.data);
    }
    

    Update:

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to use socketstream's RPC over websockets abstraction while using Angular for MVC.
I'm using some client-side JavaScript code to pull a lot of JSON data from
I know there is an implementation of VNC using WebSockets (http://novnc.com) but that still
I would like to connect to to irc, icq, sip, etc services using WebSockets.
Received a strange error when using secure websockets in Safari with Webbit server. Error
I am using the Mojolicious::Lite module to run a websockets server to handle the
I'm currently using em-websocket with Event Machine. It works great, but I also want
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
I know its possible to use WebSockets within C# using a console application running
I'm trying to work with the phpwebsocket library at http://code.google.com/p/phpwebsocket/ I'm using the version

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.