作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
由于 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/
我是一名优秀的程序员,十分优秀!