14

The W3 announced that they intend to deprecate the BlobBuilder API in preference for the new Blob API.

If I am already using BlobBuilder in a JavaScript app, how can I convert to using this new Blob API? The old WebKitBlobBuilder is still available in the latest WebKit (and Chrome Canary), but it will soon be removed. Before you could write something like this:

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

How could this be rewritten to use the new Blob constructor? Thank you.

neave
  • 2,482
  • 1
  • 26
  • 18
  • This article has a good explanation: http://updates.html5rocks.com/2012/06/Don-t-Build-Blobs-Construct-Them – neave Jun 11 '12 at 21:40

3 Answers3

13

Passing an ArrayBuffer to the Blob constructor appears to be deprecated, so:

var dataView = new DataView(arrayBuffer);
var blob = new Blob([dataView], { type: mimeString });
Alf Eaton
  • 5,226
  • 4
  • 45
  • 50
  • This gives me Uncaught Error: TYPE_MISMATCH_ERR: DOM File Exception 11 Do you have a full working example? – Greg Ennis Nov 08 '12 at 19:14
  • 1
    `var arrayBuffer = new ArrayBuffer(16); var mimeString = "text/plain"; var dataView = new DataView(arrayBuffer); var blob = new Blob([dataView], { type: mimeString }); console.log(blob);` – Alf Eaton Nov 13 '12 at 16:07
2

From what the specs says it should be as simple as this. Just check the examples of the page you posted.

var blob = new Blob(arrayBuffer);

[Constructor, Constructor((ArrayBuffer or Blob or DOMString)

GillesC
  • 10,647
  • 3
  • 40
  • 55
  • Ah thanks. It also looks like Blob() takes a simple Array object, not an ArrayBuffer - at least in Chrome. Not sure if this is how it's supposed to be, but Chrome errors otherwise. – neave May 02 '12 at 14:27
1
var blob = new Blob([arrayBuffer], {type: mimeString});
emk
  • 221
  • 3
  • 4
  • 2
    This now produces a warning: "ArrayBuffer values are deprecated in Blob Constructor. Use ArrayBufferView instead." – Alf Eaton Jul 24 '12 at 16:09
  • 2
    `var blob = new Blob([new Uint8Array(arrayBuffer)], {type: mimeString});` will fix that for you. – ellisbben Nov 28 '12 at 00:10