How To Replace The Deprecated Blobbuilder With The New Blob Constructor?
Since Blobbuilder is deprecated and I have recently decided to use a new facial recognition API I am having a hard time switching over to just 'blob'. function dataURItoBlob(dataUR
Solution 1:
Switching from BlobBuilder
to Blob
is quite straightforward. Try the following backwards-compatible code (the stuff in the catch
block is your original code):
...
try {
returnnewBlob([ab], {type: mimeString});
} catch (e) {
// The BlobBuilder API has been deprecated in favour of Blob, but older// browsers don't know about the Blob constructor// IE10 also supports BlobBuilder, but since the `Blob` constructor// also works, there's no need to add `MSBlobBuilder`.varBlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder;
var bb = newBlobBuilder();
bb.append(ab);
return bb.getBlob(mimeString);
}
}
Solution 2:
Blob = (function() {
var nativeBlob = Blob;
// Add unprefixed slice() method.if (Blob.prototype.webkitSlice) {
Blob.prototype.slice = Blob.prototype.webkitSlice;
}
elseif (Blob.prototype.mozSlice) {
Blob.prototype.slice = Blob.prototype.mozSlice;
}
// Temporarily replace Blob() constructor with one that checks support.returnfunction(parts, properties) {
try {
// Restore native Blob() constructor, so this check is only evaluated once.Blob = nativeBlob;
returnnewBlob(parts || [], properties || {});
}
catch (e) {
// If construction fails provide one that uses BlobBuilder.Blob = function (parts, properties) {
var bb = new (WebKitBlobBuilder || MozBlobBuilder), i;
for (i in parts) {
bb.append(parts[i]);
}
return bb.getBlob(properties && properties.type ? properties.type : undefined);
};
}
};
}());
Include this before you are going to use Blobs and you'll be able to use Blob constructors in browsers that only support the deprecated BlobBuilder.
Post a Comment for "How To Replace The Deprecated Blobbuilder With The New Blob Constructor?"