I want to convert Object Array to Formdata in Angular. My object array can contain any key value pair and image as well.
I, then have to send this formdata via POST API to backend(Express).
[{uid: "", image: File, description: "store", price: "800"}
{uid: "f9b37f48-cff0-44f1-aa9f-fb9766bde90b", description: "wooden sandals", price: "100"}
{uid: "dd5adebf-06c6-4d6c-b005-2fcb0a2ca161", description: "blanket", image: File}]
I've tried alot of options but anable to achieve this.
How this conversion can be done?
Or, if I can directly send this Object Array to POST call from Angular?
EDIT 1:
image: File
is a File object:
And upload image:
onUploadImage(imageInput: HTMLInputElement, i, id: string) {
if(imageInput.files && imageInput.files[0]) {
this.image = imageInput.files[0]
if(this.collections[i]['image'] === "" || typeof this.collections[i]['image'] === 'string') this.collections[i]['image'] = this.image
let result = this.collectionsToAdd.some(item => item['uid'] === id)
if(result) {
const index = this.collectionsToAdd.indexOf(this.collectionsToAdd.find(item => item.uid === id))
this.collectionsToAdd[index]['image'] = this.image
}
else {
const obj = {
uid: id,
image: null
}
obj.uid = id
obj.image = this.image
this.collectionsToAdd.push(obj)
}
this.reader.readAsDataURL(imageInput.files[0])
this.reader.onload = (event) => {
this.collections[i]['imagePath'] = event.target.result
}
}
}
Thanks in advance!