Currently, I want to upload a file to our server. I can do it with  Http from @angular/http lib. 
But now, we want to change using HttpClient to align with the current project. I did search around, I could not find a solution on how to add a form data to the post request using HttpClient?
[Update] the code that i used to upload file by http
let formData: FormData = new FormData();
formData.append('file', file, file.name);
this._http.post(base_api_url + uploadFileApi, formData)
Could someone help me on that?
Thank so much.
HttpClient class provides a base class for sending/receiving the HTTP requests/responses from a URL. It is a supported async feature of . NET framework. HttpClient is able to process multiple concurrent requests. It is a layer over HttpWebRequest and HttpWebResponse.
Here is the Angular service to do that:
import { Injectable } from '@angular/core';
@Injectable()
export class UploadService {
  constructor(private http: HttpClient) {}
  uploadFile(file: File): Observable<any> {
    const formData = new FormData();
    formData.append('file', file);
    const headers = new HttpHeaders({ 'enctype': 'multipart/form-data' });
    return this.http.post(apiUrl, formData, { headers: headers });
  }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With