Following is my code for file upload.
<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!");
});
}
Though, I can see the file details in 'imagedata', 'data' is coming empty. I am not able to figure out why 'data' is empty. That's why the backend call is failing.
Following is the request payload going to the server after submit:
{item_file: {}, delivery_id: "eeb9422e-9805-48eb-a8be-ad2e27f3f643", description: "test description"}