The Ajax binary method for sending and receiving binary byte stream data

Source: Internet
Author: User

The Ajax binary method for sending and receiving binary byte stream data

In the HTML5 Ajax 2.0 standard, many functions of Ajax are enhanced, including sending FormData data and uploading data progress bars. But in fact, 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(512);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.responseTextif (arrayBuffer) {var byteArray = new Uint8Array(arrayBuffer);for (var i = 0; i < byteArray.byteLength; i++) {}}};oReq.send(null);

Of course, the above settings can only be of the text type. If it is of the Blob type, you can do 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 earlier version of the browser, you can load the binary 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 != 200) return '';return req.responseText;}

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

The above section describes how to send and receive binary byte stream data through Ajax on the Win7 taskbar. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.