import { HttpClient , HttpHeaders , HttpResponse } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs' ; @ Injectable ( ) export class ImageUploadService { constructor ( private http ) { } public uploadImage ( url , image , headers ? , partName = 'image' , customFormData ? , withCredentials ? ) { if ( ! url || url === '' ) { throw new Error ( 'Url is not set! Please set it before doing queries' ) ; } const formData = new FormData ( ) ; if ( customFormData ) { for ( const key of Object . keys ( customFormData ) ) { formData . append ( key , customFormData [ key ] ) ; } } formData . append ( partName , image ) ; return this . http . post ( url , formData , { withCredentials , headers , observe : 'response' } ) ; } }