【问题标题】:Angular 8 415 Unsupported Media Type,Content type 'application/json' not supported +Spring BootAngular 8 415 不支持的媒体类型,不支持内容类型“应用程序/json”+Spring Boot
【发布时间】:2021-02-19 12:20:27
【问题描述】:

我的问题, 我可以使用 Postman(Spring Boot) 上传文件。服务有效,但是当我使用 angular 上传文件时,出现错误。(15 Unsupported Media Type,Content type 'application/json' not supported)

春天*

  @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);
  }

【问题讨论】:

    标签: angular spring spring-boot


    【解决方案1】:

    问题是您发送的headers。 使用 append 或以下构造函数

        const headers = new Headers();
        headers.append('Content-Type', 'multipart/form-data');
        headers.append('Accept', 'text/plain');
    

     headers: new HttpHeaders({
                "Accept": "text/plain",
                'Content-Type', 'multipart/form-data'
            })
    

    然后像这样创建帖子选项

    const httpOptions = {headers: headers};
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-26
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 2013-01-13
      • 2013-08-08
      • 1970-01-01
      • 2017-06-21
      相关资源
      最近更新 更多