错误“对象存储库当前不支持Blob值".尝试将文件存储在indexedDB中时 [英] Error "The object store currently does not support blob values" when trying to store file in indexedDB
本文介绍了错误“对象存储库当前不支持Blob值".尝试将文件存储在indexedDB中时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用IndexedDB在JavaScript中进行存储,以存储Blob. 这是我的代码
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;
})();
我正在这样使用它:
var storage = new Storage(function () {
var blob = new Blob(['FooBar']);
storage.putFile('test', blob);
});
在Chrome上,我得到了:
On Chrome i get :
DataCloneError: Failed to execute 'put' on 'IDBObjectStore': The object store currently does not support blob values.
在Firefox上,我得到了:
On Firefox i get :
[Exception... "Data provided to an operation does not meet requirements." code: "0" nsresult: "0x80660005 (DataError)" location: "<unknown>"]
我不明白怎么了.
推荐答案
我确实找到了问题的答案.好吧,我确实找到了解决该问题的方法:如何在Firefox中永久存储文件?"
I did find the answer to my question. Well, I did find somthing to resolve the problem : "how to store files permanently in Firefox ?"
我发现了一个FileSystem后备: https://github.com/ebidel/idb.filesystem. js
I found a FileSystem fallback : https://github.com/ebidel/idb.filesystem.js
效果很好.
这篇关于错误“对象存储库当前不支持Blob值".尝试将文件存储在indexedDB中时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文