I want to send an uploaded image file along with data entered by the user to the backend which is implemented on JAVA.
`const payload = {
id: null,
name : Name,
email : Email
};
//data.append("myjsonkey", );
await fetch('http://localhost:8080/student/insertStudent', {
method: 'POST',
body: JSON.stringify(payload),
headers : {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})`
Using this implementation I was able to send data to backend using POST request. I now want to attach an image file in the payload to be recieved at the backend. The Code written for the image uploading is
`fileChangedHandler = (event) => {
this.setState({selectedFile: event.target.files[0]})
}
uploadHandler = () => {
console.log(this.state.selectedFile)
}
render() {
return (
<div>
<input type="file" onChange={this.fileChangedHandler}/>
<button onClick={this.uploadHandler}>Upload!</button>
</div>
);
}`
Any help will be highly appreciated.