I'm trying to make a storage in javascript using IndexedDB to store blobs. Here is my code
var Storage = (function () {
function Storage(callback) {
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.OIndexedDB || window.msIndexedDB, IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.OIDBTransaction || window.msIDBTransaction, dbVersion = 1.0;
this.URL = window.URL || window.webkitURL;
var request = indexedDB.open('files', dbVersion);
var self = this;
request.onerror = function (e) {
console.log("Error creating/accessing IndexedDB database");
};
request.onsuccess = function (e) {
console.log("Success creating/accessing IndexedDB database");
self.db = request.result;
self.db.onerror = function (event) {
console.log("Error creating/accessing IndexedDB database");
};
// Interim solution for Google Chrome to create an objectStore. Will be deprecated
if (self.db.setVersion) {
if (self.db.version != dbVersion) {
var setVersion = self.db.setVersion(dbVersion);
setVersion.onsuccess = function () {
self.createObjectStore(self.db);
callback();
};
} else {
callback();
}
} else {
callback();
}
};
// For future use. Currently only in latest Firefox versions
request.onupgradeneeded = function (e) {
self.createObjectStore(e.target.result);
};
}
Storage.prototype.createObjectStore = function (db) {
console.log('Creating objectStore');
db.createObjectStore('files');
};
Storage.prototype.putFile = function (fileName, blob) {
console.log('Putting file in IndexedDB');
var transaction = this.db.transaction(['files'], 'readwrite');
var put = transaction.objectStore('files').put(blob, fileName);
};
return Storage;
})();
I'm using it like that :
var storage = new Storage(function () {
var blob = new Blob(['FooBar']);
storage.putFile('test', blob);
});
On Chrome i get :
DataCloneError: Failed to execute 'put' on 'IDBObjectStore': The object store currently does not support blob values.
On Firefox i get :
[Exception... "Data provided to an operation does not meet requirements." code: "0" nsresult: "0x80660005 (DataError)" location: "<unknown>"]
I don't understand what is wrong.
I did find the answer to my question. Well, I did find somthing to resolve the problem : "how to store files permanently in Firefox ?"
I found a FileSystem fallback : https://github.com/ebidel/idb.filesystem.js
It works well.
User contributions licensed under CC BY-SA 3.0