Created
February 9, 2021 22:34
-
-
Save danielwestendorf/86de79fc5e11853883ccae22e543234a to your computer and use it in GitHub Desktop.
dexie-blob-error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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