2017-07-18 62 views
0

您好我想在我的反应应用程序中使用axios发布多部分数据,但它不会得到张贴和花费很长时间后给我错误差不多多5分钟“(失败)的净::: err_CONNECTION_RESET,我不知道是怎么回事错在这里,下面是代码片段coudn't上传多部分数据的反应应用程序使用axios

功能,使reqeuset

handleClick(event){ 
let self = this; 
    if(this.state.filesToBeSent.length>0){ 
      const formData = new FormData(); 
      formData.append('file',this.state.filesToBeSent[0][0]); 
      let jsonObject = new Object; 
     jsonObject.itemId="1262"; 
     jsonObject.module="Breakfix"; 
     jsonObject.filename=("yyyyy"); 
     jsonObject.filepath="c:\\abc\\"; 
     jsonObject.createdOn=Math.round(new Date().getTime()/1000.0); 
     jsonObject.createdBy="3"; 

      formData.append("attachment", JSON.stringify(jsonObject)); 
     let filesArray = this.state.filesToBeSent; 
    axios.post(GLOBAL.uploadFile, 
      formData 
); 
} 
else{ 
alert("Please upload some files first"); 
} 
} 
**code written on express to route the post to the actual API** : 
function uploadFiles(request, response) { 
let payload = request.signedCookies['access_token']; 
payload.apikey = "d2c-234"; 
const secret = 'my-secret'; 
const signed = jwt.sign(payload, secret, { 
algorithm: 'HS256', 
expiresIn: '7d' 
}); 
let config = { 
headers: {'x-auth-token': signed 
} 
}; 
let data={} 
if(!_.isEmpty(request.body)) { 
data = request.body; 
} 
axios.post("https://abc-myapp.net/serviceManagement/rest/uploadBreakfixImage/",data,config) 
.then(uploadResponse => { 
    response.status(200).json(uploadResponse); 
}).catch(function(error) { 
    console.error(error.stack); 
}); 
} 
when putting console on the express side it seems request doesn't have the payload, what am i doing wrong here ?? 

回答

1

你可以用” t将JSON数据与文件或任何其他附件一起发布,您可以将它作为表单数据发布到您的后端,表单数据作为multi-p将艺术数据传送到具有相关边界的服务器以下是供您参考的示例代码。您可以将json数据与formData一起作为key,value对传递。

let data = new FormData(); 

data.append('itemId', '1262'); 
data.append('module', 'Breakfix'); 
data.append('filename', 'yyyyy'); 
data.append('filepath', 'c:\\abc\\'); 
data.append('upload', fileData, fileName) 


axios.post(url, data) 
    .then(response => console.log(response)) 
    .catch(errors => console.log(errors)); 

希望这会有所帮助。快乐编码!

+0

嗨@Ravindra,我正在串化该JSON对象,把所有的关键字命名为“附件”,然后再解析它在API端使用JSON解析器,它工作正常,当我试图通过POSTMAN客户端提交数据。 –

+0

所以,您的意思是,您将整个JSON内容作为一个键值对中的字符串发送给名为附件的键。然后检查HTTP头并在这里发布。所以我可以帮助你。 –

+0

接受方:application/json,text/plain,*/* Content-Length:196667 Content-Type:multipart/form-data; border = ---- WebKitFormBoundarymCXU9DZi4uU9kQA4 Cookie:access_token ='这里是我的访问令牌' –

相关问题