i




 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New features in XMLHttpRequest
12-27-2011, 08:06 AM
Post: #1
New features in XMLHttpRequest
This tutorial explains about some of the new features in XMLHttpRequest, especially those that can be used for working with files


Fetching data

ArrayBuffer responses

An ArrayBuffer is a generic fixed-length container for binary data. They are super handy if you need a generalized buffer of raw data


Code:
var xhr = new XMLHttpRequest();
xhr.open('GET', '/path/image.png', true);
xhr.responseType = 'arraybuffer';

xhr.onload = function(e) {
  var uInt8Array = new Uint8Array(this.response);  
  ...
};

xhr.send()



Blob responses:-

If you want to return a Blob and need to manipulate any of the file's bytes, use xhr.responseType='blob'


Code:
window.URL = window.URL || window.webkitURL;  // Take care of vendor prefixes.

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

xhr.onload = function(e) {
  if (this.status == 200) {
    var blob = this.response;

    var img = document.createElement('img');
    img.onload = function(e) {
      window.URL.revokeObjectURL(img.src);
    };
    img.src = window.URL.createObjectURL(blob);
    document.body.appendChild(img);
    ...
  }
};

xhr.send();



Quote


Possibly Related Threads...
Thread: Author Replies Views: Last Post
  Uploading file with Javascript and XMLHTTPRequest haider.ali 4 47,757 03-17-2013 09:32 PM
Last Post: rahmatei



User(s) browsing this thread: 1 Guest(s)