Search code examples
angularangular-httpclient

Send a binary file via Angular HttpClient


I want to send an http POST request with a binary data from a file. I get a successful server respond when I do it via postman->Body->Binary->Choose file. see image:

enter image description here

But I can not figure out how to do it via Angular HttpClient. How can I finish the following:

set processImage(event) {
    console.log(event);
    let files: FileList = event.target.files;
    let file = files[0]; 
    //send the file as a binary via httpClient
    ....

Solution

  • Finally got it to work. Here's the code for future reference for anyone in need:

    processImage(event) {
        console.log(event);
        let files: FileList = event.target.files;
        let file : File = files[0];
        this.http.post(URL, file).subscribe(
          (r)=>{console.log('got r', r)}
        )