Ajax requests a picture. There is a problem with displaying the picture in binary form.

I want to request a picture through ajax

vm.ajaxGet("/img/logo.png", function(data) {
    //data,ajax
    var img = document.createElement("img");
    img.onload = function(e) {
        window.URL.revokeObjectURL(img.src); // 
    };
    img.src = window.URL.createObjectURL(new Blob([data], { type: "image/png" }));
    document.body.appendChild(img);
});

cannot display the picture. What"s wrong with the god?

resolved:

var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.responseType = "blob";//
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var blod = this.response;
            var src = URL.createObjectURL(blod);
        }
    }
};
xhr.send();
Mar.28,2021

refer to this article: ajax requests and processes binary streams (picture)
https://codeshelper.com/q/10.


1. Check whether your file stream is a binary stream or a base64 stream
2. window.URL.revokeObjectURL (img.src) is it because this line of code is affected, comment out

Menu