Home > Back-end >  URL.createObjectURL() not working properly
URL.createObjectURL() not working properly

Time:09-19

In my web extension I need to convert a file from a Blob to an array so I can store it in web extension storage. However when I want to convert it back and use it as an image it doesn't really work.

// converting to a normal array to store in storage
const myArray = Array.from(new Uint8Array(await myBlob.arrayBuffer()));
chrome.storage.local.set({image: myArray});

now from my background script:

const myArray = (await chrome.storage.local.get()).image;

console.log(URL.createObjectUrl(new Blob(Uint8Array.from(myArray), { type: "image/png" })));

The console.log produces a link which brings me to a white image, which is not what the image is. And I do know for sure that myArray transfers correctly through the storage since even if I try to do URL.createObjectURL() without storage it produces the same result.

And if I don't convert it into an array and back it does work so I am unsure what is the problem.

CodePudding user response:

I'm pretty sure it should be:

new Blob([Uint8Array.from(myArray)], { type: "image/png" })

instead of

new Blob(Uint8Array.from(myArray), { type: "image/png" })
  • Related