不要在Chrome中使用BlobBuilder(在OSX Chrome,Firefox 12,Safari 6,iOS Chrome,iOS
Safari中测试过):
var xhr = new XMLHttpRequest();xhr.open('GET', 'doodle.png', true);xhr.responseType = 'arraybuffer';// Process the response when the request is ready.xhr.onload = function(e) { if (this.status == 200) { // Create a binary string from the returned data, then enpre it as a data URL. var uInt8Array = new Uint8Array(this.response); var i = uInt8Array.length; var binaryString = new Array(i); while (i--) { binaryString[i] = String.fromCharCode(uInt8Array[i]); } var data = binaryString.join(''); var base64 = window.btoa(data); document.getElementById("myImage"). + base64; }};xhr.send();注意 : 此代码已超过7年的历史了。
尽管它仍然可以在大多数浏览器中正常运行,但这是基于@TypeError的建议的更新版本,该建议仅适用于更现代的浏览器 ,但iOS
Safari可能会例外 (可能支持或可能不支持
responseType = 'blob'-请确保进行测试!):
var xhr = new XMLHttpRequest();xhr.open('get', 'doodle.png', true);// Load the data directly as a Blob.xhr.responseType = 'blob';xhr.onload = () => { document.querySelector('#myimage').src = URL.createObjectURL(this.response);};xhr.send();


