将地址转成blob格式(服务器下运行)

<div id="forAppend" class="demo"></div>
<script>
    var eleAppend = document.getElementById("forAppend");
    window.URL = window.URL || window.webkitURL;
    if (typeof history.pushState == "function") {
        var xhr = new XMLHttpRequest();
        xhr.open("get", "test/images/1.gif", 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); eleAppend.appendChild(img); } }
xhr.send();
}

else {
eleAppend.innerHTML
= '<p style="color:#cd0000;">浏览器不给力,请更换浏览器后重试</p>';
}

</script>

 

posted @ 2017-05-08 11:49  贺小鸣  阅读(4042)  评论(0编辑  收藏  举报
为尊重他人劳动成果,转载/摘抄请标明来源!