0

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.

Ronnie Oosting
  • 1,252
  • 2
  • 14
  • 35
Jyoti
  • 21
  • 1
  • 5
  • Just a heads-up. The Content-Type `application/file` is not a valid Media-Type, see : http://www.iana.org/assignments/media-types/media-types.xhtml .I assume either Angular or your Service understands that and maps it to json. You aren't even sending a file anyways. The proper way to send files is in the answer provided by Sachila. – Agash Thamo. Aug 10 '18 at 09:19
  • Hi Jyoti, this question/answer on Stack overflow might be helpful [File Upload using AngularJS](https://stackoverflow.com/questions/18571001/file-upload-using-angularjs) – Roy Scheffers Aug 10 '18 at 09:19

2 Answers2

1

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);
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
0

I am assuming that your data is a formData,if not then create form data something like that before calling httpClient....

const data= new FormData();
data.append('file', data);  //note that this data is file not json data...

if your are sending formData then below function should work...

fileUpload(data){


          let headers = new Headers({ 
    });

//add any header if you need to for authentication 

return this.httpClient.post('api/data_loader/file/',data, headers);
}