krise
krise

Reputation: 535

Looping through formdata in typescript

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?

Upvotes: 3

Views: 3030

Answers (1)

Daniel
Daniel

Reputation: 11182

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.

Upvotes: 1

Related Questions