Book Image

NW.js Essentials

By : Benoit
Book Image

NW.js Essentials

By: Benoit

Overview of this book

If you are an experienced Node.js developer who wants to create amazing desktop applications using NW.js, this is the book for you. Prior knowledge of HTML5, jQuery, and CSS is assumed.
Table of Contents (11 chapters)
10
Index

XMLHttpRequest and BLOBs


The one thing you should remember when dealing with XMLHttpRequest on NW.js applications is that local files, called through the file:// or app:// protocols, return 0 as the HTTP response.

var xhr = new XMLHttpRequest(), blob;
xhr.open('GET', 'myBlob.jpg', true);
xhr.responseType = 'blob';
xhr.addEventListener('load', function () {
  if (xhr.status !== 200) return;
  blob = xhr.response;
  // Do something with blob
}, false);
xhr.send();

In the preceding example, we try to get a BLOB ( which, in our case, is an image), but the code will stop when checking for xhr.status !== 200 as the returned status for local files will indeed be 0. I take this opportunity to show you how to store BLOBs in an IndexedDB object store:

var xhr = new XMLHttpRequest(), blob;
xhr.open('GET', 'maggieAvatar.jpg', true);
xhr.responseType = 'blob';
xhr.addEventListener('load', function () {
  maggieAvatar = xhr.response;
  customers.add({
    name: 'Maggie Griffin',
    email: 'maggie@example...