Skip to content Skip to sidebar Skip to footer

Javascript - Create A File Out Of Json Object And Use It In A Formdata

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: s

Solution 1:

Well, 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);

Solution 2:

Try to add this in your headers

const headers = {
  processData: false,
  contentType: false 
}

this.http.post(this.base_url + '/files', formData, headers)

Post a Comment for "Javascript - Create A File Out Of Json Object And Use It In A Formdata"