Отправить (Данные JSON, файл)
Угловой HTML:
<form [formGroup]="profileForm" (ngSubmit)="onSubmit()">
<input type="hidden" name="size" value="1000000">
<div>
<input type="file" (change)="onFileSelected($event)">
</div>
<div>
<input type="text" formControlName="clientName" placeholder="client Name">
<input type="text" formControlName="lastName" placeholder="Last Name">
<input type="text" formControlName="age" placeholder="Age">
<button type="submit" name="upload">POST</button>
</div>
</form>
Угловой ts:
profileForm = new FormGroup({
clientName : new FormControl(''),
lastName : new FormControl(''),
age : new FormControl('')
});
selectedFile = null;
public data:clientClass = new clientClass();
onFileSelected(event) {
this.selectedFile = event.target.files[0];
console.log(this.selectedFile);
}
onSubmit() {
let object = this.profileForm.value;
const payload = new FormData();
payload.append("addClient",JSON.stringify(object));
payload.append("image", this.selectedFile, this.selectedFile.name);
this.http.post(`http://localhost:8080/yourAPI/uploadTestEntity`,payload, {
responseType: 'text'}).subscribe(
(object) => {
this.profileForm.reset();
});
}
Файл модулей приложения:
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports:[ BrowserModule, FormsModule,ReactiveFormsModule ]
})
Rest API:
@PostMapping("/uploadTestEntity")
public String uploadTestEntity(
@RequestParam("addClient") String clientNameEntityNew,
@RequestParam(value = "image") MultipartFile image) {
try {
ObjectMapper mapper = new ObjectMapper();
testEntity testEntity = mapper.readValue(clientNameEntityNew,testEntity.class);
testEntity.setImage(image.getBytes());
TestEntityService.save(testEntity);
return "File uploaded successfully! -> filename = "+ image.getOriginalFilename();
} catch ( Exception e) {
return "FAIL! Maybe You had uploaded the file before or the file's size > 500KB";
}
}
2 - отправка файла и данных в виде параметрови получите в качестве параметров в Rest API:
Угловой html:
<form (ngSubmit)="onSubmit()">
<input type="hidden" name="size" value="1000000">
<div>
<input type="file" name="image" (change)="onFileSelected($event)">
</div>
<div>
<input id="textauthor" [(ngModel)]="clientName" name="clientName" placeholder="Name">
<input id="textauthor" [(ngModel)]="lastName" name="lastName" placeholder="last
Name">
<input id="textauthor" [(ngModel)]="age" name="age" placeholder="age">
<button type="submit" name="upload">POST</button>
</div>
</form>
Угловой ts:
clientName:string;
lastName:string;
age:string;
resData: any;
selectedFile = null;
onFileSelected(event) {
this.selectedFile = event.target.files[0];
console.log(this.selectedFile);
onSubmit() {
const payload = new FormData();
payload.append('clientName', this.clientName);
payload.append('lastName', this.lastName);
payload.append('age', this.age);
payload.append('image', this.selectedFile, this.selectedFile.name);
this.http.post(`http://localhost:8080/apiHorsesClub/uploadTestEntity`,
payload).subscribe((data: any) => { this.resData = data;console.log(this.resData);
});
}
API отдыха:
@PostMapping("/uploadTestEntity")
public String uploadTestEntity(
@RequestParam("clientName") String clientName ,
@RequestParam("lastName") String lastName
@RequestParam("age") String age
,@RequestParam(value = "image") MultipartFile image) {
try {
testEntity testEntity = new testEntity();
testEntity.setImage(image.getBytes());
testEntity.setClientName(clientName);
testEntity.setLastName(lastName);
testEntity.setAge(age);
return "File uploaded successfully! -> filename = "+ image.getOriginalFilename();
} catch ( Exception e) {
return "FAIL! Maybe You had uploaded the file before or the file's size > 500KB";
}
}