2016-06-08 46 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