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!