我的问题是,我可以用postman(spring boot)上传文件。服务可以工作,但是当我用angular上传文件时,我得到了一个错误。(15不支持媒体类型,内容类型'application/json'不支持)
Spring*
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<HttpStatus> saveProfil(@RequestParam("file") MultipartFile file)
**棱角分明的
const httpOptions = {
headers: new HttpHeaders().set('Content-Type','multipart/form-data')
}
export class ApiService {
constructor(private httpClient: HttpClient) { }
postFile(path: string, body: any): Observable<any> {
return this.httpClient.post(path, body, httpOptions).pipe(catchError(this.formatError));
}
}
onSubmit(event) {
const file = event.target.files[0];
this.fileUpload = file;
const formData:FormData= new FormData();
formData.append('file',this.fileUpload);
this.uploadService.saveProfile(formData).subscribe(res => {.....});
}
saveProfile(formData: FormData): Observable<any> {
return this.apiService.postFile(apiHost + '/Profil', formData);
}
1条答案
按热度按时间ujv3wf0j1#
问题在于
headers
您正在发送。使用append或以下构造函数或
然后像这样创建post选项