C0mpl3x
C0mpl3x

Reputation: 532

How do I create file with json inside?

I'm trying to create a new File() and write json to it but I Don't really understand how to. I've looked at the documentation but I don't understand what BlobPart is.

When I tried this:

const file = new File(this.myFrom.getRawValue(), 'json');

I got error :

Failed to construct 'File': The object must have a callable @@iterator property.

I need to append the file to formData like this:

formData.append('file', file);

Upvotes: 1

Views: 1072

Answers (1)

ADyson
ADyson

Reputation: 62059

You're nearly right. You just need to

a) put the content inside an array, as per the documentation

b) set the content type through the options

This is the corrected version of the code:

const file = new File([JSON.stringify(this.configForm.getRawValue())], 'payapp_init_json.json', {type: 'application/json'});

Upvotes: 1

Related Questions