Skip to content

Instantly share code, notes, and snippets.

@danielwestendorf
Created February 9, 2021 22:34
Show Gist options
  • Save danielwestendorf/86de79fc5e11853883ccae22e543234a to your computer and use it in GitHub Desktop.
Save danielwestendorf/86de79fc5e11853883ccae22e543234a to your computer and use it in GitHub Desktop.
dexie-blob-error
<!doctype html>
<html>
<head>
<!-- Include Dexie -->
<script src="https://unpkg.com/dexie@latest/dist/dexie.js"></script>
<script>
//
// Define your database
//
var db = new Dexie("friend_database");
db.version(1).stores({
friends: 'name,shoeSize'
});
var xhr = new XMLHttpRequest(),
blob;
xhr.open("GET", "https://dexie.org/assets/images/dexie-logo-icon.svg", true);
// Set the responseType to blob
xhr.responseType = "blob";
xhr.addEventListener("load", function () {
if (xhr.status === 200) {
console.log("Image retrieved");
// Blob as response
blob = xhr.response;
console.log("Blob:" + blob);
db.friends.put({name: "Nicolas", shoeSize: 8, blob: blob}).then (function(){
//
// Then when data is stored, read from it
//
return db.friends.get('Nicolas');
}).then(function (friend) {
//
// Display the result
//
console.log("Nicolas has shoe size " + friend.shoeSize);
console.log(friend.blob)
}).catch(function(error) {
//
// Finally don't forget to catch any error
// that could have happened anywhere in the
// code blocks above.
//
alert ("Ooops: " + error);
console.error(error)
});
}
}, false);
// Send XHR
xhr.send();
</script>
</head>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment