比我尝试的第一个解决方案
我使用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 ----->确定
内容处置:表单数据; name =“ file”; filename =“ servlet.pdf”
内容类型:应用程序/ pdf


--------------------------- 7da24f2e50046--
内容类型多部分/表单数据; boundary = --------------------------- 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)
内容类型多部分/表单数据; boundary = --------------------------- 7da24f2e50046 ------>确定

----------------------------- 7e018a1b2079a ------> Ko
内容处置:表单数据; name =“文件”; filename =“ servlet.pdf”
内容类型:应用程序/ pdf


Ç¥¾}«s

最佳答案

第二种方法是正确的方法,除了没有设置内容类型标头。使用FormData对象发出请求时,将自动正确地设置它。

10-04 19:43