FormData upload in angular4 application
So after going through a lot of solution i stumbled upon this issue
It says do not add the Content-Type to the header. So from my request header i removed
headers.append('Content-Type', 'multipart/form-data');
Thanks!
I'll give a reference with Angular 6 and Spring Boot.
Angular 6 :
onSubmit(){
const formdata: FormData = new FormData();
console.log(this.currentFileUpload)
formdata.append('uploadedFile', this.currentFileUpload);
formdata.append('description',"My Desc");
formdata.append('companyName',"ABC")
formdata.append('numberOfRecords',"2")
console.log(formdata.get('uploadedFile'))
this.asyncService.makeUploadRequest('file/upload',formdata).subscribe(response => {
console.log(response)
if(response.responseCode == '00'){
console.log('Successfully uploaded')
}
else {
console.log("error")
}
});
}
makeUploadRequest(method, body) : Observable<any> {
const url = "http://localhost:8097" + "/" + method;
const headers = new Headers();
this.token="Bearer"+" "+localStorage.getItem('token');
headers.append('Authorization', this.token);
// No need to set 'content type'
const options = new RequestOptions({headers: headers});
return this.http.post(url, body, options).pipe(
map((response : Response) => {
var json = response.json();
return json;
})
);
}
Spring Boot API :
@RestController
@RequestMapping(value = { "/file" })
public class FileController {
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public Response uploadFile(@RequestParam("uploadedFile") MultipartFile uploadedFileRef){
System.out.println(uploadedFileRef);
}
}