[英]XMLHttpRequest not working for IE 11 for file download
var formdata = new FormData(); var formdata = new FormData();
var xhr = null;
if(typeof XMLHttpRequest != "undefined"){
xhr = new XMLHttpRequest();
}
else if(typeof window.ActiveXObject != "undefined"){
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP.4.0");
}
catch(e){
try {
xhr = new ActiveXObject("MSXML2.XMLHTTP");
}
catch(e){
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
xhr = null;
}
}
}
}
xhr.open("GET",url, true); ///fileUploadTester/FileUploader
xhr.send(formdata);
xhr.responseType = "arraybuffer";
xhr.onload = function(e) {
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0) // If Internet Explorer, return version number
{
var urlIE = URL;
window.location = urlIE;
}
else
{
window.location = this.responseURL;
}
};
}
The above code is from my javaScript method when i call that method my requirement is to download file for user.上面的代码来自我的 javaScript 方法,当我调用该方法时,我的要求是为用户下载文件。 In java i have a method to generate file and add it to response在 java 中,我有一种生成文件并将其添加到响应的方法
For other browsers i am able to call the method and get response back but for IE 11 i am not able to do.对于其他浏览器,我可以调用该方法并获得响应,但对于 IE 11,我无法做到。 Any Solution for this or any errors in my code?对此有任何解决方案或我的代码中有任何错误吗?
Got the Answer The way i was getting browser name was wrong. 得到答案我获取浏览器名称的方式是错误的。 Once i got correct browser name using javascript it stated working 一旦我使用javascript获得正确的浏览器名称,它就会开始工作
FormData() is not supported by ie11.u can build the formdata string by yourself. ie11不支持FormData(),可以自己构建formdata字符串。
function sendData(data) {
console.log('Sending data');
const XHR = new XMLHttpRequest();
const urlEncodedDataPairs = [];
// Turn the data object into an array of URL-encoded key/value pairs.
for (const [name, value] of Object.entries(data)) {
urlEncodedDataPairs.push(`${encodeURIComponent(name)}=${encodeURIComponent(value)}`);
}
// Combine the pairs into a single string and replace all %-encoded spaces to
// the '+' character; matches the behavior of browser form submissions.
const urlEncodedData = urlEncodedDataPairs.join('&').replace(/%20/g, '+');
// Define what happens on successful data submission
XHR.addEventListener('load', (event) => {
alert('Yeah! Data sent and response loaded.');
});
// Define what happens in case of an error
XHR.addEventListener('error', (event) => {
alert('Oops! Something went wrong.');
});
// Set up our request
XHR.open('POST', 'https://example.com/cors.php');
// Add the required HTTP header for form data POST requests
XHR.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
// Finally, send our data.
XHR.send(urlEncodedData);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.