如何用新的Blob构造函数替换已弃用的BlobBuilder? [英] How to replace the deprecated BlobBuilder with the new Blob constructor?
问题描述
由于Blobbuilder已弃用且我最近决定使用新的面部识别API,因此我很难切换到blob。
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(dataURI, callback) {
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0) {
byteString = atob(dataURI.split(',')[1]);
} else {
byteString = unescape(dataURI.split(',')[1]);
}
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// 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);
}
// write the ArrayBuffer to a blob, and you're done
var BlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder;
var bb = new BlobBuilder();
bb.append(ab);
return bb.getBlob(mimeString);
}
我尝试将其转换为:
// write the ArrayBuffer to a blob, and you're done
var Blob = window.URL || window.webkitURL;
var bb = new Blob();
/*var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = window.URL.createObjectURL(blob);
document.body.appendChild(link);*/
/*var BlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder;
var bb = new BlobBuilder();
bb.append(ab);*/
return bb.getBlob(mimeString);
}
但我一直得到未捕获TypeError:对象#< ; URL>在控制台中没有方法'getBlob'
。不确定我错过了什么。如果我尝试使用 bb.append(ab);
我得到未捕获TypeError:对象#< Blob>在控制台中没有方法'append'
。
But i keep getting Uncaught TypeError: Object #<URL> has no method 'getBlob'
in the console. Not sure what I am missing. If i try to use bb.append(ab);
I get Uncaught TypeError: Object #<Blob> has no method 'append'
in the console.
推荐答案
从切换BlobBuilder
到 Blob
非常简单。尝试以下向后兼容的代码( catch
块中的内容是您的原始代码):
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 {
return new Blob([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`.
var BlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder;
var bb = new BlobBuilder();
bb.append(ab);
return bb.getBlob(mimeString);
}
}
这篇关于如何用新的Blob构造函数替换已弃用的BlobBuilder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!