1. 程式人生 > >使用XMLHttpRequest處理異步請求返回的圖片等二進制文件

使用XMLHttpRequest處理異步請求返回的圖片等二進制文件

folder float blob als ner func 圖片 rand wid

封裝的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處理異步請求返回的圖片等二進制文件