ajax二進制流亂碼圖片解決方法


僅供自己參考

參考博客

在請求成功的地方 添加以下代碼:

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();


免責聲明!

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



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