最近遇到一个问题:html 页面上有一个 a 标签,点击它会向服务器发送一个 ajax 请求,想通过点击返回的链接让浏览器下载文件,试图将返回的链接赋值给 window.location,但却被浏览器拦截了。
之后通过构建一个隐藏的 iframe 来下载,从而解决拦截问题,代码实现如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| $(".downloadDesignPng,.downloadDesignPdf", tr).click(function () { $(this).hide();
var type; var className = $(this).attr("class"); if("downloadDesignPng" == className) { type = 0; } else if("downloadDesignPdf" == className) { type = 1; } else { return; }
$("#downloadTip").text("正在下载,请耐心等待");
$.ajax({ type: "POST", url: "/print/print/getPrintDesignDownloadUrl.do", dataType: "json", data: { orderNo : orderNo, type : type }, success: function(data) { $("#downloadTip").empty(); $("." + className, tr).show();
if(data.url) { var IFrameRequest = document.createElement("iframe"); IFrameRequest.id = "IFrameRequest"; IFrameRequest.src = "http:" + data.url; IFrameRequest.style.display = "none"; document.body.appendChild(IFrameRequest); } else { alert("参数错误"); } } }); });
|