Skip to content

Instantly share code, notes, and snippets.

@saurshaz
Created July 11, 2016 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saurshaz/8b0391f3bbfb1563a38c8518d79afdf9 to your computer and use it in GitHub Desktop.
Save saurshaz/8b0391f3bbfb1563a38c8518d79afdf9 to your computer and use it in GitHub Desktop.
get data from indexeddb
// IndexedDB
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.OIndexedDB || window.msIndexedDB,
IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.OIDBTransaction || window.msIDBTransaction,
dbVersion = 1.0;
// Create/open database
var request = indexedDB.open("local", dbVersion),
db,
createObjectStore = function (dataBase) {
// Create an objectStore
console.log("Creating objectStore")
dataBase.createObjectStore("files");
},
getFile = function () {
// Create XHR
var xhr = new XMLHttpRequest()
// Open a transaction to the database
var readWriteMode = typeof IDBTransaction.READ_WRITE == "undefined" ? "readwrite" : IDBTransaction.READ_WRITE;
var transaction = db.transaction(["files"], readWriteMode);
// Retrieve the file that was just stored
transaction.objectStore("files").get("5F1682A4-1FB2-439C-9EE9-654E137BEC0E").onsuccess = function (event) {
var imgFile = event.target.result;
console.log("Got files!" + imgFile);
console.log(imgFile);
// // Get window.URL object
// var URL = window.URL || window.webkitURL;
// // Create and revoke ObjectURL
// var imgURL = URL.createObjectURL(imgFile);
// // Set img src to ObjectURL
// var imgElephant = document.getElementById("elephant");
// imgElephant.setAttribute("src", imgURL);
// // Revoking ObjectURL
// imgElephant.onload = function() {
// window.URL.revokeObjectURL(this.src);
// }
};
};
request.onerror = function (event) {
console.log("Error creating/accessing IndexedDB database");
};
request.onsuccess = function (event) {
console.log("Success creating/accessing IndexedDB database");
db = request.result;
db.onerror = function (event) {
console.log("Error creating/accessing IndexedDB database");
};
// Interim solution for Google Chrome to create an objectStore. Will be deprecated
if (db.setVersion) {
if (db.version != dbVersion) {
var setVersion = db.setVersion(dbVersion);
setVersion.onsuccess = function () {
createObjectStore(db);
getFile();
};
}
else {
getFile();
}
}
else {
getFile();
}
}
// For future use. Currently only in latest Firefox versions
request.onupgradeneeded = function (event) {
createObjectStore(event.target.result);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment