We want to send the image file as multipart / form to the backend, we are trying to use the html form to receive the file and send the file as formData, here are the codes
export default class Task extends React.Component { uploadAction() { var data = new FormData(); var imagedata = document.querySelector('input[type="file"]').files[0]; data.append("data", imagedata); fetch("http://localhost:8910/taskCreationController/createStoryTask", { mode: 'no-cors', method: "POST", headers: { "Content-Type": "multipart/form-data" "Accept": "application/json", "type": "formData" }, body: data }).then(function (res) { if (res.ok) { alert("Perfect! "); } else if (res.status == 401) { alert("Oops! "); } }, function (e) { alert("Error submitting form!"); }); } render() { return ( <form encType="multipart/form-data" action=""> <input type="file" name="fileName" defaultValue="fileName"></input> <input type="button" value="upload" onClick={this.uploadAction.bind(this)}></input> </form> ) } }
The error in the backend is a "nested exception." org.springframework.web.multipart.MultipartException: failed to parse multi-hearted servlet request, nested exception - java.io.IOException: org.apache.tomcat.util.http.fileupload. FileUploadException: The request was rejected because a multiline border was not found. "
After reading this, we tried to set the border for the headers in the sample:
fetch("http://localhost:8910/taskCreationController/createStoryTask", { mode: 'no-cors', method: "POST", headers: { "Content-Type": "multipart/form-data; boundary=AaB03x" + "--AaB03x" + "Content-Disposition: file" + "Content-Type: png" + "Content-Transfer-Encoding: binary" + "...data... " + "--AaB03x--", "Accept": "application/json", "type": "formData" }, body: data }).then(function (res) { if (res.ok) { alert("Perfect! "); } else if (res.status == 401) { alert("Oops! "); } }, function (e) { alert("Error submitting form!"); }); }
This time an error in the backend: Servlet.service () for the servlet [dispatcherServlet] in the context with the path [] threw an exception [Error processing request; the nested exception is java.lang.NullPointerException] with the root cause.
Will we add a polyhedral border to the right? Where should it be? Perhaps we are mistaken at first, because we are not receiving data about multipart / form-data. How can we get it right?