如何从 IE 上的 GET XMLHttpRequest 响应下载文件?
How download file from GET XMLHttpRequest response on IE?
我正在使用 XMLHttpRequest GET 请求进行文件下载:
var filename = 'data.zip';
var request = new XMLHttpRequest();
request.open('GET', "/solfwk/srp/exportImportService/export", true);
request.responseType = 'blob';
request.setRequestHeader("ALFSSOAuthNToken", token);
request.setRequestHeader("Content-Type", "application/json");
request.setRequestHeader("Accept", "application/json");
request.onload = function() {
var link = document.createElement('a');
document.body.appendChild(link);
link.href = window.URL.createObjectURL(request.response);
link.download = filename;
if(window.navigator.userAgent.indexOf("MSIE") > 0) {
link.text = "Start...";
$('#messageExport').empty().append(link);
} else {
link.click();
}
$.unblockUI();
};
request.send();
对于非 IE 浏览器 link.click()
有效,但对于 IE 无效。知道如何解决这个问题吗?
对于 IE 使用:
var request = new ActiveXObject("Microsoft.XMLHTTP");
要确定 XmlHttpRequest 在浏览器中是否可用,请使用以下代码:
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
我使用 msSaveBlob 或 msSaveOrOpenBlob 函数找到了这样的解决方案:http://msdn.microsoft.com/en-us/library/ie/hh779016(v=vs.85).aspx
我正在使用 XMLHttpRequest GET 请求进行文件下载:
var filename = 'data.zip';
var request = new XMLHttpRequest();
request.open('GET', "/solfwk/srp/exportImportService/export", true);
request.responseType = 'blob';
request.setRequestHeader("ALFSSOAuthNToken", token);
request.setRequestHeader("Content-Type", "application/json");
request.setRequestHeader("Accept", "application/json");
request.onload = function() {
var link = document.createElement('a');
document.body.appendChild(link);
link.href = window.URL.createObjectURL(request.response);
link.download = filename;
if(window.navigator.userAgent.indexOf("MSIE") > 0) {
link.text = "Start...";
$('#messageExport').empty().append(link);
} else {
link.click();
}
$.unblockUI();
};
request.send();
对于非 IE 浏览器 link.click()
有效,但对于 IE 无效。知道如何解决这个问题吗?
对于 IE 使用:
var request = new ActiveXObject("Microsoft.XMLHTTP");
要确定 XmlHttpRequest 在浏览器中是否可用,请使用以下代码:
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
我使用 msSaveBlob 或 msSaveOrOpenBlob 函数找到了这样的解决方案:http://msdn.microsoft.com/en-us/library/ie/hh779016(v=vs.85).aspx