Search code examples
angularangular-services

I want to upload file using angular 6 and connect that with API, but its taking default type "application/json", i want to change that to file format


I am doing in this way:

fileUpload(data){

      let headers = new HttpHeaders().set('file', data);
         headers.append('Content-Type', 'application/file');
      let file_upload =  {
        headers: headers,
      };

    console.log(data, "file upload")
    return this.httpClient.post('api/data_loader/file/', file_upload);
  }

Error:

Unsupported media type "application/json" in request.

Expected Result: I want to change this default media type to file format.

Note: I am using Angular6.


Solution

  • Need to use the form data in order to upload a file.

    const formData: FormData = new FormData();
    formData.append('file', data, data.name); 
    this.httpClient.post('api/data_loader/file/', formData);