Book Image

NW.js Essentials

Book Image

NW.js Essentials

Overview of this book

Table of Contents (17 chapters)
NW.js Essentials
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
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...