Ajax methods for sending and receiving binary byte-stream data _ajax related

Source: Internet
Author: User

The HTML5 AJAX 2.0 standard enhances many features of Ajax, including sending Formdata data, uploading data progress bars, and more. In practice, however, Ajax can send binary data in bytes.

Send binary data

var oreq = new XMLHttpRequest ();
Oreq.open ("POST", url, true);
Oreq.onload = function (oevent) {
//uploaded.
};
var blob = new blob ([' abc123 '], {type: ' Text/plain '});
Oreq.send (BLOB);

Or

var myarray = new Arraybuffer (in);
var longint8view = new Uint8array (myarray);
for (var i=0; i< longint8view.length; i++) {
longint8view[i] = i% 255;
}
var xhr = new XMLHttpRequest;
Xhr.open ("POST", url, false);
Xhr.send (myarray);

Receive binary data

var oreq = new XMLHttpRequest ();
Oreq.open ("Get", "/myfile.png", true);
Oreq.responsetype = "Arraybuffer";
Oreq.onload = function (oevent) {
var arraybuffer = oreq.response;//Note:not Oreq.responsetext
if (arraybuffer) {
var bytearray = new Uint8array (arraybuffer);
for (var i = 0; i < bytearray.bytelength; i++) {
}
}
};
Oreq.send (NULL);

Of course, if the setting can only be text type, if it is a BLOB type, then the following

var oreq = new XMLHttpRequest ();
Oreq.open ("Get", "/myfile.png", true);
Oreq.responsetype = "Arraybuffer";
Oreq.onload = function (oevent) {
var blob = new Blob ([Oreq.response], {type: "Image/png"});
// ...
};
Oreq.send ();

Or

var oreq = new XMLHttpRequest ();
Oreq.open ("Get", "/myfile.png", true);
Oreq.responsetype = "blob";
Oreq.onload = function (oevent) {
var blob = oreq.response;
// ...
};
Oreq.send ();

If you are using an older version of the browser, then the load binary can be as follows

function Load_binary_resource (URL) {
var req = new XMLHttpRequest ();
Req.open (' Get ', url, false);
XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
req.overridemimetype (' Text\/plain; Charset=x-user-defined ');
Req.send (null);
if (Req.status!=) return ";
return req.responsetext;
}

Note: X-user-defined tells the browser not to parse the data

The above is a small set to introduce the WIN7 task bar Ajax send and receive the binary Word throttling data methods related knowledge, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.