I am trying to upload file and other form field contents from my Angular 2 front end to Spring back end. But somehow I am not able to do it. Here is my code:
app.component.ts
fileChange(e){
this.fileList = e.target.files;
}
uploadPdf(){
if(this.fileList.length>0){
let file: File = this.fileList[0];
let formData:FormData = new FormData();
formData.append('uploadFile', file, file.name);
formData.append('info',this.model);
console.log(file.size);
let headers = new Headers();
headers.append('Accept', 'application/json');
let options = new RequestOptions({ headers: headers });
this.http.post(this.url,formData, options)
/*.map((res: Response) => res.json())*/
.catch(error => Observable.throw(error))
.subscribe(
data =>{
this.data = data;
console.log(this.data);
}
,
error => console.log(error)
)
}
}
app.cmoponent.html
<h1 class="text-center">
PDF Viewer and Uploader Example
</h1>
<div class="text-center">
<form enctype="multipart/form-data">
<div class="form-group">
<label for="name">Name: </label>
<input type="text" id="name" class="form-control" name="name" [(ngModel)]="model.name">
</div>
<div class="form-group">
<label for="email">Email: </label>
<input type="email" id="email" class="form-control" name="email" [(ngModel)]="model.email">
</div>
<div class="form-group">
<label for="pdfInput">Select File: </label>
<input type="file" id="pdfInput" class="form-control" name="pdfInput" (change)="fileChange($event)">
</div>
<div class="form-group">
<button type="button" class="btn btn-success" (click)="uploadPdf()">Upload File!</button><span> </span>
<button type="button" class="btn btn-success" (click)="printData()">Print to Console!</button>
</div>
</form>
</div>
model.ts
export class Model{
name: string;
email: string;
}
Now in Backend:
ExampleModel.java
public class ExampleModel {
private String name;
private String email;
//Getters and Setters
MainController.java
@RequestMapping(value = "/file",method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<String> addUser(@RequestParam("uploadFile") MultipartFile file, @RequestParam("info") ExampleModel model){}
So, how to get that info
labeled data in the spring controller? Above I have shown my attempt which is wrong so, what is the correct approach to get other form fields data?
How the Controller method with annotations should be defined or is there other way to send data(both file + form fields) from Angular 2?
Here is my solution :
It is very important to leave the Content-Type empty. If you set the 'Content-Type' to 'multipart/form-data' the upload will not work !
upload.component.html
upload.component.ts
i'm tryning to use formData to send both myobject and a file form angular 2 to spring mvc deploed on websphere:
ao.component.ts is:
service is:
}
ang my spring service is:
Now using tomcat and mysql it works fine , but once deploed on web sphere i get the problem: [03/01/18 10:21:50:148 GMT] 0000008c srt W com.ibm.ws.webcontainer.srt.SRTServletResponse setHeader SRVE8094W: AVERTISSEMENT : Impossible de définir l'en-tête. La réponse a déjà été validée.
and in console: POST http://localhost:4200/gtaows/services/uploadfile 415 (Unsupported Media Type)
thx,
You need to use
@RequestPart
instead of@RequestParam
and setconsumes
attribute:You also need to adjust your
FormData
object: