3 回答
TA贡献1873条经验 获得超9个赞
XMLHttpRequest的
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', 'http://RestServiceURL-Returns Image', true);
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlhttp.responseType = 'arraybuffer/blob';
xmlhttp.send();
以3种方式创建blob图像。
window.URL。createObjectURL
FileReader(caniuse)
Base64String
xmlhttp.onload = function() {
var blob = new Blob([this.response], {type: 'image/png'});
console.log(blob, blob.type, this.response, typeof this.response);
var image = document.getElementById('my-image');
1)image.src = window.URL.createObjectURL(blob);
2)var fileReader = new window.FileReader();
fileReader.readAsDataURL(blob);
fileReader.onloadend = function() {
image.src = fileReader.result;
}
3)var base64String = btoa(String.fromCharCode.apply(null, new Uint8Array(this.response)));
image.src = 'data:image/png;base64,'+base64String;
};
将ArrayBuffer转换为Blob转换为ArrayBuffer
1)var dataView = new DataView(arrayBuffer);
var blob = new Blob([dataView], { type: mimeString });
2)fileReader.readAsArrayBuffer(blob);
var arrayBuffer;
fileReader.onload = function() {
arrayBuffer = this.result;
};
添加回答
举报