1

Is there a way to send FormData and a json object at the same time in http.request of angular 2+? I need the solution for angular2+, not to angularjs.

let data = {id: 1, name: 'test'};
let formData = new FormData();
formData.append('fileData', file); //file from inputfile

let headers = new Headers();
headers.append('Accept', 'application/json');

let options =  new RequestOptions({ headers: headers });
options.method = 'POST';
options.body = data; //data is my object

//options.formData= formData; //formData is my FormData with file data to upload

this.http.request(url, options);
1
  • 1
    I finally found a proper way to upload a file and send some JSON within the same request and made a proper answer here: stackoverflow.com/questions/39693966/… Commented Nov 21, 2017 at 8:16

1 Answer 1

3

You have to pass the file and the JSON object on the "formData" object like this example:

public submitForm(picture: File, data: any): Observable<any> {
 const formData = new FormData();
 formData.append('picture', picture);
 formData.append('data', JSON.stringify(data));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.