Skip to content

Instantly share code, notes, and snippets.

@robnyman
Created January 23, 2013 22:13
Show Gist options
  • Save robnyman/4614614 to your computer and use it in GitHub Desktop.
Save robnyman/4614614 to your computer and use it in GitHub Desktop.
(function () {
// 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("elephantFiles", dbVersion),
db,
createObjectStore = function (dataBase) {
// Create an objectStore
console.log("Creating objectStore")
dataBase.createObjectStore("images");
},
getImageFile = function () {
// Code for getting images as a blob through WebActivities from Camera, Gallery etc
// Then:
putImageInDb(blob);
},
putImageInDb = function (blob) {
console.log("Putting images in IndexedDB");
// Open a transaction to the database
var transaction = db.transaction(["images"], IDBTransaction.READ_WRITE);
// Put the blob into the dabase
var put = transaction.objectStore("images").put(blob, "image");
};
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");
};
getImageFile();
}
// 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