【问题标题】:IE11 : Content-Type = false in IE11 it doesn't workIE11:在 IE11 中 Content-Type = false 它不起作用
【发布时间】:2016-10-08 16:20:13
【问题描述】:

我尝试过的第一个解决方案 我使用 beginQuoteFileUnquoteUpload1 方法,它为我创建了一个良好的边界和良好的 Content-Type 但是当我收到文件时。文件已损坏:(

var formData = new FormData(); 
formData.append('file', document.getElementById('file').files[0], document.getElementById('file').files[0].name);

function beginQuoteFileUnquoteUpload1(data)
{
    // Define a boundary, I stole this from IE but you can use any string AFAIK
    var boundary = "---------------------------7da24f2e50046";
    var xhr = null;
    if (window.XMLHttpRequest || window.ActiveXObject) {
	if (window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}else{
		xhr = new XMLHttpRequest(); 
	}
    }else{
	alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
	return;
    }
    var body = '--' + boundary + '\r\n'
             + 'Content-Disposition: form-data; name="file";'
             + 'filename="'+document.getElementById('file').files[0].name+'"\r\n'
             + 'Content-type: application/pdf\r\n\r\n'
             + data + '\r\n'
             + '--'boundary + '--';

    var url ="https://gedcreditor.mycloud.by/Myproject/ws/rest/sendemail/";
    url+=document.getElementById('file').files[0].name;
    url+="/a/a/a";	
    xhr.open("POST", url, true);
    xhr.setRequestHeader(
        "Content-type", "multipart/form-data; boundary="+boundary);
    xhr.setRequestHeader('Authorization','Bearer ' + JWTtoken);//Our test server does not accept JWt, once we use AXA server, we will test the JWT
    xhr.setRequestHeader('ApiKey','lRABmnmS_H1Ej9yaowxqwEsuBbkxkgrzx-C1Jji_HfnJyKywR8NeuSkIJbhutfNg');	
    xhr.onreadystatechange = function ()
    {
        if (xhr.readyState == 4 && xhr.status == 200)
            alert("File uploaded!");
    }
    xhr.send(body);
}

beginQuoteFileUnquoteUpload1(formData);

******************************结果****************** ******************

------------------7da24f2e50046 -----> 好的 内容处置:表单数据;名称=“文件”;文件名=“servlet.pdf” 内容类型:application/pdf

---------------------------7da24f2e50046-- 内容类型多部分/表单数据;边界=---------------7da24f2e50046 ----> 好的

================================================ ==================================================== =================================================

第二个解决方案确实有效,因为防火墙阻止了我,因为我在方法和结果下方没有相同的边界。

var formData = new FormData(); 
formData.append('file', document.getElementById('file').files[0], document.getElementById('file').files[0].name);
function beginQuoteFileUnquoteUpload(data)
{
    // Define a boundary, I stole this from IE but you can use any string AFAIK
    var boundary = "---------------------------7da24f2e50046";
    var xhr = null;
    if (window.XMLHttpRequest || window.ActiveXObject) {
	if (window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}else{
		xhr = new XMLHttpRequest(); 
	}
    }else{
	alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
	return;
    }
    var url ="https://gedcreditor.mycloud.by/GEDCREDITOR_01_06/ws/rest/sendemail/";
    url+=document.getElementById('file').files[0].name;
    url+="/a/a/a";	
    xhr.open("POST", url, true);
    xhr.setRequestHeader(
        "Content-type", "multipart/form-data; boundary="+boundary);
    //xhr.setRequestHeader('Authorization','Bearer ' + JWTtoken);//Our test server does not accept JWt, once we use AXA server, we will test the JWT
   // xhr.setRequestHeader('ApiKey','lRABmnmS_H1Ej9yaowxqwEsuBbkxkgrzx-C1Jji_HfnJyKywR8NeuSkIJbhutfNg');	
    xhr.onreadystatechange = function ()
    {
        if (xhr.readyState == 4 && xhr.status == 200)
            alert("File uploaded!");
    }
    xhr.send(data);
}

beginQuoteFileUnquoteUpload(formData);

******************************结果****************** ******************

Clé Valeur 内容类型多部分/表单数据;边界=---------------7da24f2e50046 ------> 好的

------------------7e018a1b2079a ------> Ko 内容处置:表单数据;名称=“文件”;文件名="servlet.pdf" 内容类型:application/pdf

Ç¥¾}«s

【问题讨论】:

    标签: javascript jquery ajax xmlhttprequest


    【解决方案1】:

    第二种方法是正确的,只是你没有设置内容类型标题。当您使用 FormData 对象发出请求时,它会自动正确设置。

    【讨论】:

    • 很奇怪,因为当我们不设置内容类型时,数据是正确的,但是http请求的header中没有内容类型,所以WAF阻塞了我们
    • 以下错误:drive.google.com/…
    • 我只是尝试在 IE11 中发送一个表单数据对象而不设置内容类型标题,它生成了带有边界的标题。我不确定是什么导致了您的问题
    • 您好穆萨,感谢您的帮助。你能分享我你的测试吗?我会在我身边试试。顺便说一句,这是我的 IE 版本:11.0.9600.18314 版本更新:11.0.31 (KB3154​​070) 你的是什么?
    猜你喜欢
    • 2018-01-11
    • 2018-06-14
    • 2016-03-11
    • 2018-10-25
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    相关资源
    最近更新 更多