I'm trying to simulate a file upload by providing file content instead of serving the real file.
So - I'm doing something like this:
uploadFile(jsonContent: string, otherParams: string) {
const formData = new FormData();
formData.append('jsonContent', data, 'fileName.json');
formData.append('deal_id', dealId);
return this.http.post(this.base_url + '/files', formData);}
I'm not seeing the content being sent to the API. Any advice? What I'm doing wrong?
I'm trying to simulate a file upload by providing file content instead of serving the real file.
So - I'm doing something like this:
uploadFile(jsonContent: string, otherParams: string) {
const formData = new FormData();
formData.append('jsonContent', data, 'fileName.json');
formData.append('deal_id', dealId);
return this.http.post(this.base_url + '/files', formData);}
I'm not seeing the content being sent to the API. Any advice? What I'm doing wrong?
Share Improve this question edited Jun 11, 2018 at 6:05 Eliran Eliassy asked Jun 10, 2018 at 13:49 Eliran EliassyEliran Eliassy 1,60012 silver badges25 bronze badges 2- can I see your data variable value in formData.append('jsonContent', data, 'fileName.json');? – wobsoriano Commented Jun 11, 2018 at 6:13
-
@sorxrob it's just a js object...
{name: 'foo'}
– Eliran Eliassy Commented Jun 12, 2018 at 8:22
2 Answers
Reset to default 5Well, I've found a solution for this.
In typescript you can create new File()
and pass a blob object into it.
Now u can actually create a file in your client side and send it as part as your FormData.
here is the code:
const st = JSON.stringify(json);
const blob = new Blob([st], { type: 'application/json' });
const file = new File([ blob ], 'FileName.json');
const formData = new FormData();
formData.append('file', file, 'FileName.json');
formData.append('deal_id', dealId);
Try to add this in your headers
const headers = {
processData: false,
contentType: false
}
this.http.post(this.base_url + '/files', formData, headers)