前端Blob对二进制流数据的处理方式

var xhr = new XMLHttpRequest();    
     xhr.open("post", "/login/getCaptcher?t=" + Math.random() + "&account=" + username, true);
     xhr.responseType = "blob";
     xhr.onload = function() {
         if (this.status == 200) {
             //var blob = this.response;  // this.response也就是请求的返回就是Blob对象
      var blob = new Blob([this.response],{type:"image/jpeg"});  
             var img = document.getElementById("codeImg");
      
      console.log( this.response)
             img.onload = function(e) {
        console.log(111)
               window.URL.revokeObjectURL(img.src); // 清除释放
             };
             //console.log( blob)
             img.src = URL.createObjectURL(blob);//创建对象URL
         }
     }
     xhr.send();

  Content-Type(Mime-Type):参考http://tool.oschina.net/commons

  Blob目前只兼容IE10.

原文地址:https://www.cnblogs.com/yiyi17/p/7544280.html