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 ??
嗨@Ravindra,我正在串化該JSON對象,把所有的關鍵字命名爲「附件」,然後再解析它在API端使用JSON解析器,它工作正常,當我試圖通過POSTMAN客戶端提交數據。 –
所以,您的意思是,您將整個JSON內容作爲一個鍵值對中的字符串發送給名爲附件的鍵。然後檢查HTTP頭並在這裏發佈。所以我可以幫助你。 –
接受方:application/json,text/plain,*/* Content-Length:196667 Content-Type:multipart/form-data; border = ---- WebKitFormBoundarymCXU9DZi4uU9kQA4 Cookie:access_token ='這裏是我的訪問令牌' –