1

In javascript I want to download(get) binary files from server and use its bytes. I use this code:

$.get('/file.mp4', function(data) {
    var bytes = new Uint8Array(data.length);
    for (var i=0; i<data.length; i++) {
            bytes[i] = data.charCodeAt(i);
        }
    });

But there is a problem: some characters of data variable have ASCII code grater than 255 (like "ą" --> ASCII:261)!! and charCodeAt(i) return 65533 for them also when i use console.log(data[i]) output is "�".

I tested "ą".charCodeAt(0) and output was 261 so I guess that problem is in data that received by get method not in charCodeAt method. Is there an alternative method to download binary files??

5
  • 4
    did you tried Uint16Array/Uint32Array ? Commented Jul 29, 2015 at 9:42
  • you can request arrayBuffer in XMLHttpRequest Commented Jul 29, 2015 at 9:49
  • @MatteoRubini Yes I have tried them but problem has not been solved yet. Commented Jul 29, 2015 at 9:49
  • henryalgus.com/reading-binary-files-using-jquery-ajax Commented Jul 29, 2015 at 9:50
  • @JaromandaX so many thanks. The problem has been solved. Commented Jul 29, 2015 at 10:03

1 Answer 1

3

fetch data like this:

var xhr = new XMLHttpRequest();
xhr.open('GET', '/my/image/name.png', true);
xhr.responseType = 'blob';

xhr.onload = function(e) {
  if (this.status == 200) {
    // get binary data as a response
    var blob = this.response;
  }
};

xhr.send();
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.