angular 表单数据不会附加

angular form data won't append

我正在尝试使用 angular 6 上传文件。但是,我似乎无法附加我的 formData()。一点帮助。谢谢。

home.component.ts

export class HomeComponent implements OnInit {
  selectedFile: File = null;
constructor(
private _route: ActivatedRoute,
private _router: Router,
private _postService: PostService
) { }

ngOnInit() {}


onFileChanged(event) {
this.selectedFile = event.target.files[0]}

postImage() {
const uploadData = new FormData();
uploadData.append('myFile', this.selectedFile, this.selectedFile.name);
console.log(uploadData)
this._postService.postImage(uploadData)}}

home.component.html

<input style="display: none" type="file (change)="onFileChanged($event)" #fileInput>
<button (click)="fileInput.click()">Select File</button>
<button (click)="postImage()">Upload!</button>

尝试这样的事情:

DEMO

该文件已附加在 formdata 中,但您不能直接检查表单数据:

onFileChanged(event) {

    let formData = new FormData();
    this.selectedFiles = event.target.files;
    this.currentFileUpload = this.selectedFiles.item(0);


    console.log(this.currentFileUpload)
    formData.append('file', this.currentFileUpload);

    formData.append('labelName', 'test');
    formData.append('formPart', 'test');
    console.log(JSON.stringify(formData))

   formData.forEach((value,key) => {
      console.log(key+" "+value)
    });
  }