XMLHttpRequest接受流請求,顯示圖片


 

假如后台的接口,我們本來是返回一個圖片,但是有些情況下,接口直接返回一個stream,那么如何通過AJAX顯示在頁面?

var xhr = new XMLHttpRequest();    
    xhr.open("get", "http://localhost:8080/getImage", true);
    xhr.responseType = "blob";
    xhr.onload = function() {
        if (this.status == 200) {
            var blob = this.response;
            var img = document.createElement("img");
            img.src = window.URL.createObjectURL(blob);
    $("#img").html(img);
 } };
xhr.send();

細節中特別的地方就是blob,通過這個類型來處理stream

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM