第一个解决方案比我尝试过 我使用方法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 = “文件”;文件名= “servlet.pdf” 内容类型:application / pdf
--------------------------- 7da24f2e50046-- 内容类型multipart / form-data; boundary = --------------------------- 7da24f2e50046 ---->行
=============================================== ================================================== ================================================= < / p>
第二个解决方案确实有效,因为防火墙阻止了我,因为我在方法和结果下面没有相同的边界。
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 内容类型multipart / form-data; boundary = --------------------------- 7da24f2e50046 ------&gt;行
----------------------------- 7e018a1b2079a ------&gt;柯 内容处理:表格数据; NAME = “文件”;文件名= “servlet.pdf” 内容类型:application / pdf
Ç¥¾}«小号
答案 0 :(得分:1)
第二种方法是正确的方法,除了您没有设置内容类型标题。使用FormData对象发出请求时,它会自动正确设置。