Can't save blob to indexedDB
Asked Answered
H

2

8

I'm having trouble saving blob in IndexedDB, and only with blob.
If I save something else (like image as base64), everything works fine.
But with blob, there is simply empty object property saved.

Screenshot from console:

enter image description here

Code:

//prepared blob...
var openRequest = indexedDB.open("testDB",1);

openRequest.onupgradeneeded = function(e) {
    var thisDB = e.target.result;

    if(!thisDB.objectStoreNames.contains("stash")) {
        thisDB.createObjectStore("stash");
    }
}

openRequest.onsuccess = function(e) {
    db = e.target.result;

    var transaction = db.transaction(["stash"],"readwrite");
    var store = transaction.objectStore("stash");
    var tID = Date.now();

    var obj = {
        bl:blob,
        created:tID
    }
    console.log(obj);
    //add it 
    var request = store.add(obj, tID);
    request.onerror = function(e) {
        console.log("Error",e.target.error.name);
    }
    request.onsuccess = function(e) {
        console.log("success");
    }
}
openRequest.onerror = function(e) {
    //....
}

I also tried to save only blob (not wrapped as obj property), it's the same.
I can save blob to HDD, and if I console log my obj, I get:

enter image description here

So I guess, blob is valid, and problem is in adding it to indexedDB. I'm new to blob/indexedDB, and probably doing some noob mistake.

Can someone please advise, what am I doing wrong?

PS: no error messages at all

Hollishollister answered 30/5, 2015 at 14:53 Comment(4)
The state of binary data in the browser >IndexedDB has many Blob bugs in Chrome.Diane
@wOxxOm ok, tnx for pointing that outHollishollister
The Chrome developer console may not be showing Blobs when inspecting the stores/indexes. Have you tried writing code to read the value back out and inspect it programatically?Mayorga
@JoshuaBell no, when I tried to read it (calling it from code, and do something with it) it was empty too...if that's what you are asking...some changes were made to chromium in the meantime, maybe they fixed this too...I can't test it anymoreHollishollister
M
9

Very old question, but there is support now for saving Blobs in IndexedDb:

// Create an example Blob object
var blob = new Blob(['blob object'], {type: 'text/plain'});

try {
    var store = db.transaction(['entries'], 'readwrite').objectStore('entries');

    // Store the object  
    var req = store.put(blob, 'blob');
    req.onerror = function(e) {
        console.log(e);
    };
    req.onsuccess = function(event) {
        console.log('Successfully stored a blob as Blob.');
    };
} catch (e) {
    var reader = new FileReader();
    reader.onload = function(event) {
        // After exception, you have to start over from getting transaction.
        var store = db.transaction(['entries'], 'readwrite').objectStore('entries');

        // Obtain DataURL string
        var data = event.target.result;
        var req = store.put(data, 'blob');
        req.onerror = function(e) {
            console.log(e);
        };
        req.onsuccess = function(event) {
            console.log('Successfully stored a blob as String.');
        };
    };
    // Convert Blob into DataURL string
    reader.readAsDataURL(blob);
}

As of posting this, the referenced Document was last updated on: Last updated 2019-03-20 UTC.

Mugwump answered 5/1, 2021 at 18:47 Comment(0)
S
4

You can convert Blob or File object to ArrayBuffer object or binarystring and then save it. Convert it back to Blob after you read from indexedDB.

//prepared blob...
blobToBlob2(blob, saveBlob2);

function blobToBlob2(blob, callback){
    var reader = new FileReader();
    reader.readAsArrayBuffer(blob);
    reader.onload = function(e) {
        callback({
            buffer: e.target.result,
            type: blob.type
        });
    };
}

function blob2ToBlob(blob2){
    return new Blob([blob2.buffer],{type:blob2.type});
}

function saveBlob2(blob2){
    //..... code
    var obj = {
        bl:blob2,
        created:tID
    }
    var request = store.add(obj, tID);
    //..... code
}
Sauter answered 1/11, 2015 at 12:32 Comment(1)
I'm completely out of this project right now, but planing to go back to it when i found time (it was half-year ago). The whole point of blob-indexedDB thing was to save large images (SS), instead of using dataURIs to chrome.storage. ThanxHollishollister

© 2022 - 2024 — McMap. All rights reserved.