2016-06-08 49 views
0

第一种解决方案比我试过 我使用beginQuoteFileUnquoteUpload1方法,它创建了一个良好的边界和良好的Content-Type,但是当我收到文件时。该文件已损坏:(IE11:Content-Type = false在IE11中它不起作用

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) { 
 
\t if (window.ActiveXObject) { 
 
\t \t try { 
 
\t \t \t xhr = new ActiveXObject("Msxml2.XMLHTTP"); 
 
\t \t } catch(e) { 
 
\t \t \t xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
 
\t \t } 
 
\t }else{ 
 
\t \t xhr = new XMLHttpRequest(); 
 
\t } 
 
    }else{ 
 
\t alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest..."); 
 
\t 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+="https://stackoverflow.com/a/a/a"; \t 
 
    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'); \t 
 
    xhr.onreadystatechange = function() 
 
    { 
 
     if (xhr.readyState == 4 && xhr.status == 200) 
 
      alert("File uploaded!"); 
 
    } 
 
    xhr.send(body); 
 
} 
 

 
beginQuoteFileUnquoteUpload1(formData);

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

------------ ----------------- 7da24f2e50046 -----> Ok Content-Disposition:form-data; name =“file”; filename =“servlet.pdf” Content-类型:application/pdf

----------- ---------------- 7da24f2e50046-- Content-Type multipart/form-data; border = --------------------------- 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) { 
 
\t if (window.ActiveXObject) { 
 
\t \t try { 
 
\t \t \t xhr = new ActiveXObject("Msxml2.XMLHTTP"); 
 
\t \t } catch(e) { 
 
\t \t \t xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
 
\t \t } 
 
\t }else{ 
 
\t \t xhr = new XMLHttpRequest(); 
 
\t } 
 
    }else{ 
 
\t alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest..."); 
 
\t return; 
 
    } 
 
    var url ="https://gedcreditor.mycloud.by/GEDCREDITOR_01_06/ws/rest/sendemail/"; 
 
    url+=document.getElementById('file').files[0].name; 
 
    url+="https://stackoverflow.com/a/a/a"; \t 
 
    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'); \t 
 
    xhr.onreadystatechange = function() 
 
    { 
 
     if (xhr.readyState == 4 && xhr.status == 200) 
 
      alert("File uploaded!"); 
 
    } 
 
    xhr.send(data); 
 
} 
 

 
beginQuoteFileUnquoteUpload(formData);

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

CléValeur Content-Type multipart/form-data; boundary = --------------------------- 7da24f2e50046 ------> OK

-------- --------------------- 7e018a1b2079a ------> Ko Content-Disposition:form-data; NAME = “文件”;文件名=“servlet.pdf” 内容类型:应用/ PDF

Ç¥¾}«小号

回答

1

第二种方法是正确的,除了不设置内容类型报头。当您使用FormData对象进行请求时,它会自动正确设置。

+0

这很奇怪,因为当我们没有设置内容类型时,数据与边界是正确的,但是在http请求的头部没有内容类型,所以WAF阻止我们 – user3553500

+0

以下错误: https://drive.google.com/open?id=0BwACVkdzjcMEc3NWM1J0NF9pc0NYWERtajJrdV9TSmpqNHM0 – user3553500

+0

我只是尝试在IE11中发送表单数据对象而不设置内容类型标题,并且它会生成带有边界的标题。我不确定是什么原因导致你的问题 – Musa