I've an html form as follows
<form #basicForm = "ngForm" (ngSubmit) = "onClickSubmit(basicForm.value)" enctype="multipart/form-data" class="form-horizontal">
<input type="file" id="file-input" name="fileInput" ngModel>
<button type="submit"> Submit</button>
</form>
and onClickSubmit method is
onClickSubmit(data) {
console.log(data.fileInput);
this.http.post("http://localhost:8080/basicForm", data).subscribe( (ob)=>(console.log("subscribe method called")));
}
But this only logs a fakepath in browser console like C:\fakepath\User.java
not the file itself
and I'm not able to access the file from server side ,It seems like only the fake path is uploaded to the server not the file itself
FormDomain java class
public class FormDomain {
private File fileInput;
public File getFileInput() {
return fileInput;
}
public void setFileInput(File fileInput) {
this.fileInput = fileInput;
}
}
FormController
@CrossOrigin(origins="*")
@RestController
public class FormController {
@PostMapping(path="/basicForm")
public String postResponseController(
@RequestBody FormDomain loginForm) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(loginForm.getFileInput()));
String st;
while ((st = br.readLine()) != null) {
System.out.println(st);
}
return "file transfer completed successfully";
}
}
Accessing uploaded files this way results in error java.io.FileNotFoundException: C:\fakepath\User.java (No such file or directory)
.
How to send file as part form data that spring boot maps to the domain class?