本文介绍了在IndexedDB中保存ArrayBuffer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将二进制数据(在ArrayBuffer对象中)保存到IndexedDB中?

IndexedDB规范没有提到ArrayBuffer - 这是否意味着不被支持(我必须将ArrayBuffer打包为一个字符串或一个数组?

解决方案
在FF的最新版本(每晚)中,这非常简单。请参阅。

  window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || 
window.MozBlobBuilder;

var bb = new BlobBuilder();
bb.append(arrayBuffer);
var myblob = bb.getBlob();

indexedDB.open(mydatabase)。onsuccess = function(e){
var db = e.result;
var trans = db.transaction([objectstore1,objectstore2,READ_WRITE);
trans.objectStore(objectstore1)。put(myblob,somekey);
trans.objectStore(objectstore2)。put(myblob,otherkey);
};

偶:

  objectStore.put({name:Santa,age:400,height:185,img:myblob}); 

在Chrome中有相同的开放式错误:


How can I save binary data (in an ArrayBuffer object) into IndexedDB?

The IndexedDB spec doesn't mention ArrayBuffer - does that mean that is not supported (and I have to pack ArrayBuffer as a string or a an array?).

解决方案

In the latest (nightly) builds of FF this is very easy. See this bug.

window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder ||
                     window.MozBlobBuilder;

var bb = new BlobBuilder();
bb.append(arrayBuffer);
var myblob = bb.getBlob();

indexedDB.open("mydatabase").onsuccess = function(e) {
  var db = e.result;
  var trans = db.transaction(["objectstore1", "objectstore2", READ_WRITE);
  trans.objectStore("objectstore1").put(myblob, "somekey");
  trans.objectStore("objectstore2").put(myblob, "otherkey");
};

even:

objectStore.put({ name: "Santa", age: 400, height: 185, img: myblob});

There's an open bug for the same in Chrome: crbug.com/108012

这篇关于在IndexedDB中保存ArrayBuffer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 00:04