amuste / DnetIndexedDb

Blazor Library for IndexedDB DOM API
MIT License
103 stars 28 forks source link

Writing Blobs? #45

Open Tailslide opened 3 years ago

Tailslide commented 3 years ago

Trying to wrap my head around writing blobs and I'm starting to think it's not possible with DnetIndexedDb.

See: https://hacks.mozilla.org/2012/02/storing-images-and-files-in-indexeddb/

https://web.archive.org/web/20190117030510/http://robnyman.github.io/html5demos/indexeddb/

image

I'm trying to reproduce this but can't seem to create a store without indexes I just get the error "store.indexes is not iterable"

Tailslide commented 3 years ago

Success !

I was able to add a store without indexes by passing in an empty index list. However, dnetindexeddb doesn't expose the .add method that lets you specify a key so I changed the function to allow that. Then, it seemed to want to write everything as an encoded string because of JSInterop marshalling.

So, I tried the unmarshalled interop here: https://stackoverflow.com/questions/64803155/blazor-to-javascript-byte-array-interop/64804675

I was able to get the binary to javascript and convert it to a blob like:

const dataPtr = Blazor.platform.getArrayEntryPtr(item, 0, 4);
const length = Blazor.platform.getArrayLength(item);
var shorts = new Uint8Array(Module.HEAPU8.buffer, dataPtr, length);
var blob = new Blob([shorts]);

There's got to be a more direct way? Anyways proof of concept!

image

benbaker76 commented 2 weeks ago

There's got to be a more direct way? Anyways proof of concept!

I changed it to use a Stream along with DotNetStreamReference which also removes the need for unmarshalled interop.

I will start a pull request from my fork.