9

I try to create a Blob object and pass an Uint8Array to it’s constructor It works fine on chrome and firefox on windows In chrome and safari on ios however the Blod does not contain the data of the Uint8Array but the text : [object Uint8Array]

I need this to upload a canvas to the server.

Is there a workaround?

J.V.
  • 165
  • 2
  • 7
  • Sounds like that method expects string parameters but you pass in an object so it's toString() method gets called which, in JS, returns something like in your question to represent objects. – marekful Jan 15 '13 at 15:30
  • I don't think so. it works fine on a windows pc and also if I pass a normal array. it works ok. – J.V. Jan 17 '13 at 07:32

1 Answers1

11

I'm struggling with the exact same problem. When I backup the Uint8Array with an ArrayBuffer, it does work in both Safari and Chrome (not tested in other browsers yet) but Chrome prints a warning message. Chrome says I have to wrap ArrayBuffer in a DataView before passing it to Blob() constructor.

// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
    ia[i] = byteString.charCodeAt(i);
}

new Blob([ab], {type: mimeString});

Edit

The exact Chrome deprecation message is:

ArrayBuffer values are deprecated in Blob Constructor. Use ArrayBufferView instead.

Rob Juurlink
  • 4,516
  • 3
  • 20
  • 19
  • Unfortunately this doesn't seem to help on Safari 6.0.3 (7536.28.10). – mtyaka Apr 04 '13 at 13:24
  • 5
    This helped me find a solution to my problem: for a Uint8Array `u`, I passed `u.buffer` to the Blob constructor instead of `u` itself. – jtbandes Jun 17 '13 at 17:51
  • Thank you @jtbandes that fixed it for me! I was banging my head against my desk for hours :) – Oscar Godson Dec 18 '13 at 23:08
  • 1
    Be aware you don't need to fear using ArrayBuffer in a Blob constructor (and the deprecation message has been removed). See http://stackoverflow.com/questions/15293694/blob-constructor-browser-compatibility and https://bugs.webkit.org/show_bug.cgi?id=88389 – Mark Kasson May 02 '15 at 20:29
  • @jtbandes i am having a similar problem which i think is to do with Uint8Array and i think your solution might help me but Im not sure exactly how to do what you have said. can u have a look at my question to see how i can incorporate your answer thanks. http://stackoverflow.com/questions/31629868/web-audio-analyser-not-working-in-safari – Sarah Jul 25 '15 at 20:16
  • @Rob Juurlink I tried to use this solution but I think i am doing it wrong. i'm not fully educated on Blobs. Can u help me write this code with the solution you've given please: var frequencyData = new Uint8Array(analyser.frequencyBinCount); ... here is my question http://stackoverflow.com/questions/31629868/web-audio-analyser-not-working-in-safari – Sarah Jul 25 '15 at 20:48