Skip to content

Instantly share code, notes, and snippets.

@robnyman
Created February 23, 2012 17:28
Show Gist options
  • Save robnyman/1893918 to your computer and use it in GitHub Desktop.
Save robnyman/1893918 to your computer and use it in GitHub Desktop.
Retrieve stored file from IndexedDB and create an ObjectURL
// Retrieve the file that was just stored
transaction.objectStore("elephants").get("image").onsuccess = function (event) {
var imgFile = event.target.result;
console.log("Got elephant!" + 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
URL.revokeObjectURL(imgURL);
};
@MaxWright
Copy link

Great work! Your tutorial has been very helpful!

You may want to change this so that the URL is only revoked on load, otherwise, it may invalidate the URL before the image can be used.
imgElephant.onload = function () {
URL.revokeObjectURL(imgURL);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment