gpt4 book ai didi

javascript - 如何用新的 Blob 构造函数替换已弃用的 BlobBuilder?

转载 作者:数据小太阳 更新时间:2023-10-29 05:42:50 30 4
gpt4 key购买 nike

由于 Blobbuilder 已被弃用,而且我最近决定使用新的面部识别 API,因此我很难切换到“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);
}

但我不断收到 Uncaught TypeError: Object #<URL> has no method 'getBlob'在控制台中。不知道我错过了什么。如果我尝试使用 bb.append(ab);我得到 Uncaught TypeError: Object #<Blob> has no method 'append'在控制台中。

最佳答案

BlobBuilder 切换到 Blob 非常简单。尝试以下向后兼容的代码(catch block 中的内容是您的原始代码):

...
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);
}
}

关于javascript - 如何用新的 Blob 构造函数替换已弃用的 BlobBuilder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15030906/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com