码迷,mamicode.com
首页 > Web开发 > 详细

使用XMLHttpRequest处理异步请求返回的图片等二进制文件

时间:2017-08-02 16:27:22      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:cti   idt   setattr   window   接受   处理   channel   end   ready   

封装的Ajax没有接受文件的类型 所以要用传统的 XMLHttpRequest来处理

function ShowPdf() {
            var url = "/ChannelLiquidation/ShowPdf.ashx?PdfName=<%=PdfName %>&pdfFolder=<%=PdfFolder %>";
            var xhr = null;;
            if (window.XMLHttpRequest) {// code for IE7, Firefox, Opera, etc.
                xhr = new XMLHttpRequest();
            }
            else if (window.ActiveXObject) {// code for IE6, IE5
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            if (xhr != null) {
                xhr.onreadystatechange = false;
                xhr.open(‘GET‘, url, true);
                xhr.responseType = "blob";
                xhr.setRequestHeader("client_type", "DESKTOP_WEB");
                xhr.setRequestHeader("desktop_web_access_key", Math.random().toString());
                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);
                        img.style.cssText = "width: 90%; float: left";
                        img.setAttribute("onerror","this.src=‘../Assets/img/notBill.jpeg‘");
                        $("#div_imgcontainer").html(img);
                    }
                }
                xhr.send();
            }
        }
        $(function () {ShowPdf();})

使用XMLHttpRequest处理异步请求返回的图片等二进制文件

标签:cti   idt   setattr   window   接受   处理   channel   end   ready   

原文地址:http://www.cnblogs.com/ccmsalome/p/7274552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!