I am trying to loop through formdata, I have read several times that I should be able to use FormData in a for ... of ... loop, however when I try, I get the error:
Type 'FormData' is not an array type or a string type
My code:
export class NewsCreationComponent implements OnInit {
fileToUpload: File = null;
uploadImages = new FormData();
...
handleFile(event) {
this.fileToUpload = event.target.files;
this.uploadImages.append(this.fileToUpload.name, this.fileToUpload);
}
save() {
for (let up of this.uploadImages) {
this.imageService.createImage(up)
});
}
What am I doing wrong?
Move your FormData initialization uploadImages = new FormData(); to the ngOnInit() lifecycle hook, so you are sure that is is defined when your save() function is called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With