Friday, July 29

Files and images can be downloaded or uploaded using AJAX

To Download a file

var req = new XMLHttpRequest();
req.open('GET', 'file:///home/user/file.text', false); 
req.send(null);
if(req.status == 0)
  dump(req.responseText);
 
To Download Image (using binary)
function load_image(url) {
  var req = new XMLHttpRequest();
  req.open('GET', url, false);
  req.overrideMimeType('text/plain; charset=x-user-defined');
  req.send(null);
  if (req.status != 200) return '';
  return req.responseText;
}

To upload Image (using binary) 
var req = new XMLHttpRequest();
req.open("POST", url, true);
req.setRequestHeader("Content-Length", 741);
req.sendAsBinary(aBody);