僅供自己參考
在請求成功的地方 添加以下代碼:
var blob=new Blob();
blob=this.response;
既然二進制數據拿到了,那么要把它放在一個 html標簽中,並且應該是img標簽 那么:代碼應該是
var img = document.createElement("img");
img.src = window.URL.createObjectURL(blob); //有問題,將blob加載到img中 由於blob太大 會有性能影響 應該怎么在加載之后 如何釋放呢:
img.onload = function(e) {
window.URL.revokeObjectURL(img.src);//釋放。
};
然后 將img 放到一個div容器中就可以啦。
$("#imgcontainer").html(img); 是的請求處理就應該是這樣。
var xhr = new XMLHttpRequest();
xhr.open("get", url, true);
xhr.responseType = "blob";
xhr.onload = function() {
if (this.status == 200) {
var blob = this.response;
var img = document.createElement("img");
img.onload = function(e) {
window.URL.revokeObjectURL(img.src);
};
img.src = window.URL.createObjectURL(blob);
$("#imgcontainer").html(img);
} } xhr.send();