1
다음은 파일 업로드를위한 코드입니다.reactjs에서 업로드 한 파일이 작동하지 않습니다. 작동하지 않는 reactjs에서 파일 업로드에 대한 POST 호출
<form encType="multipart/form-data" action="">
<input type="file" name="fileName" defaultValue="fileName"></input>
<input type="button" value="upload" onClick={this.handleClick.bind(this)}></input>
</form>
handleClick(){
let deliveryId = this.props.params.deliveryId;
var data = new FormData();
var imagedata = document.querySelector('input[type="file"]').files[0];
data.append("data", imagedata);
console.log('Data', data);
fetch(apiBaseUrl, {
mode: 'no-cors',
method: "POST",
body: JSON.stringify({
'item_file': data,
'delivery_id': deliveryId,
'description': 'test description'
})
}).then(function (res) {
if (res.ok) {
alert("Perfect! ");
} else if (res.status == 401) {
alert("Oops! ");
}
}, function (e) {
alert("Error submitting form!");
});
}
'imagedata'에서 파일 세부 사항을 볼 수 있지만 '데이터'가 비어 있습니다. 왜 '데이터'가 비어 있는지 파악할 수 없습니다. 그렇기 때문에 백엔드 호출이 실패하는 것입니다.
는 다음 후 제출 서버로가는 요청 페이로드 :
{item_file : {}, delivery_id "eeb9422e-9805-48eb-a8be-ad2e27f3f643"설명 : "테스트 설명"}