안녕하세요, 저는 여러 날 동안 tar 파일을 업로드하려고하지만, 실패 할 때마다. 나는이 JS, UI 및 https req/res 도메인에 익숙하지 않다. 내 환경 : nodeJS, 백엔드 용 JavaScript. 프론트 엔드에 대한 ReactJS. 내 컴퓨터에서 원격 서버로 tar 파일을 업로드해야합니다.자바 스크립트에서 fetch API를 사용하여 파일 업로드
여기 파일을 업로드하기 위해 가져 오기 요청을 만들고있는 프런트 엔드 코드입니다.
export function postXCCDF() {
let _headers = {
'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryyEmKNDsBKjB7QEqu'
};
var formData = new FormData();
formData.append('type', 'file');
formData.append('file', 'C:\\MyDrive\\rules.zip');
let uri = '/rest/policy';
const options = { method: 'POST', headers: _headers, body: formData };
return fetch(`${_host}${urlPrefix}${uri}`, options)
.then(processStatus)
.then(response => response.json());
}
여기 내 백엔드 코드로 수신 할 tar 파일이 있습니다.
router.post('/policy', function(req, res) {
console.log('hey I reached here');
console.log('hemant req',req.body.formData); // prints hemant req undefined
// Dont know how to save it to some directory. ??? :(
});
내 모든 생각은 원격 위치에있는 서버에 tar 파일을 업로드하는 것입니다. 나는 다른 많은 방법들을 많이 시도해 왔지만 아무것도 도움이되지 않는다.
브라우저 devtools 콘솔 및 백엔드 서버 로그에 표시되는 오류 메시지의 텍스트를 제공하기 위해 답변을 편집/업데이트하려면 https://stackoverflow.com/review/suggested-edits/15772861을 사용하십시오. devtools의 네트워크 패널에서 요청이 실제로 전송되는지, 그리고 요청 헤더가 무엇인지 확인하고 어떤 응답이 수신되는지, 헤더가 무엇인지 확인하십시오 – sideshowbarker