Maria Gálvez
Maria Gálvez

Reputation: 307

Error: you are not receiving a MultipartFile

I have a REST service in java that has to receive a MultipartFile, but it gives an error, it says that it is not a MultipartFile that comes from angular. I leave the code to see if anyone knows what the problem is...

Angular 8:

sendFile(data: File): Observable<any>{
   const headers = new HttpHeaders().set('Content-Type', 'text/html; charset=utf-8');
   return this.http.post('http://localhost:8080/v1/on/file', data,{headers,responseType: 'text'})
    .pipe(
      tap(_ => this.log('send file')),
      catchError(this.handleError('not send file', []))
    );
  }

Java:

@RequestMapping("/file")
public MultipartFile filev1(
    @RequestParam("file") MultipartFile file){ 
    service.filereturn(file);
    return file;

}

Upvotes: 2

Views: 193

Answers (1)

Prashant Pimpale
Prashant Pimpale

Reputation: 10717

You have to pass it as FormData and not need to specify Content-Type in the request headers:

For example:

sendFile(data: File): Observable<any>{
   var _formData = new FormData();
   _formData.append('file', data);

   return this.http.post('http://localhost:8080/v1/on/file', _formData, { headers, responseType: 'text'})
    .pipe(
      tap(_ => this.log('send file')),
      catchError(this.handleError('not send file', []))
    );
}

Upvotes: 1

Related Questions