0
down vote
favorite
The first solution than I tried I use the method beginQuoteFileUnquoteUpload1 it created me a good boundary and good Content-Type but when I receive the file. The file is corrupted :(
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-Dis 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);
******************************Result**********************************
-----------------------------7da24f2e50046 -----> Ok Content-Dis name="file";filename="servlet.pdf" Content-type: application/pdf
---------------------------7da24f2e50046-- Content-Type multipart/form-data; boundary=---------------------------7da24f2e50046 ----> OK
==================================================================================================================================================
The second Solution does work because the firewall blocked me because I don't have the same boundary below the method and results.
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);
******************************Result**********************************
Clé Valeur Content-Type multipart/form-data; boundary=---------------------------7da24f2e50046 ------> OK
-----------------------------7e018a1b2079a ------> Ko Content-Dis name="file"; filename="servlet.pdf" Content-Type: application/pdf
Ç¥¾}«s