Methods of obtaining and sending binary data in Ajax

Source: Internet
Author: User

When using Ajax to get binaries from the server side, you need to set the request header with Overridemimttype so that the browser does not modify the read data as follows:

var xhr = new XMLHttpRequest ();

Xhr.onreadystatechange = function () {

if (xhr.readystate=== 4 && xhr.status = = 200) {

var imgdata = Xhr.response;

}

}

Xhr.open ("Get", url,true);

Xhr.overridemimetype ("Text/plain; Charset=x-user-defined ");

Xhr.send (NULL);

Also use Xhr.response to get binary data, not responsetext.

If you need to send binary data via Ajax post, you need to change the high order of the binary data to 0 before sending, usually adding a method to the XMLHttpRequest to implement:

XMLHttpRequest.prototype.sendAsBinary = function (datastr) {

function Bytevalue (x) {

Return x.charcodeat (0) & 0xFF;

}

var ords = Array.prototype.map.call (Datastr, Bytevalue);

var ui8a = new Uint8array (ords);

This.send (Ui8a.buffer);

}

var bindata = binary data;

Xhr.open ("POST", url);

Xhr.sendasbinary (Bindata);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.