首页 > 解决方案 > 在打字稿中循环表单数据

问题描述

我正在尝试遍历formdata,我已经读过几次我应该能够在for ... of ...循环中使用FormData,但是当我尝试时,我得到了错误:

Type 'FormData' is not an array type or a string type

我的代码:

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)
      });
}

我究竟做错了什么?

标签: angulartypescriptloopsfor-loopmultipartform-data

解决方案


将您的 FormData 初始化移动uploadImages = new FormData();ngOnInit()生命周期钩子,这样您就可以确定它是在save()调用函数时定义的。


推荐阅读