【发布时间】:2016-10-08 16:20:13
【问题描述】:
我尝试过的第一个解决方案 我使用 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 -----> 好的 内容处置:表单数据;名称=“文件”;文件名=“servlet.pdf” 内容类型:application/pdf
---------------------------7da24f2e50046-- 内容类型多部分/表单数据;边界=---------------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 内容类型多部分/表单数据;边界=---------------7da24f2e50046 ------> 好的
------------------7e018a1b2079a ------> Ko 内容处置:表单数据;名称=“文件”;文件名="servlet.pdf" 内容类型:application/pdf
Ç¥¾}«s
【问题讨论】:
标签: javascript jquery ajax xmlhttprequest