Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
    ....
like image 486
amit Avatar asked Jan 17 '18 08:01

amit


2 Answers

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)}
    )
like image 107
amit Avatar answered Nov 09 '22 21:11

amit


For Sending binary Data in Angular you can use FormData example :

let file = event.target.files[0];
let url = 'your url';
let formData = new FormData();
formData.append("myfile", file);

this.http.post(url,formData).subscribe(
  (res) => {
    console.log('response', res)
  }
)
like image 39
Jay Bhanushali Avatar answered Nov 09 '22 20:11

Jay Bhanushali