title: HTTP POST FormData from Angular2 client to Node server
body: <p>I have an angular2 client and I'm trying to send a file to my NodeJS server via http using a FormData object.</p>
  
  <p><strong>upload.html</strong></p>
  
  <pre><code>...
  &lt;input #fileinput type="file" [attr.multiple]="multiple ? true : null" (change)="uploadFile()" &gt;
  ...
  </code></pre>
  
  <p><strong>upload.component.ts</strong></p>
  
  <pre><code>uploadFile() {
      let files = this.fileinput.nativeElement.files;
  
      if (files.length == 0) return;
  
      this.formData = new FormData();
  
      for (var i = 0; i &lt; files.length; i++) {
          console.log("Appended file: " + files[i].name + " File object:" + files[i]); // Output: Appended file: simple.txt File object:[object File]
          this.formData.append(files[i].name, files[i]);
      }
      console.log("uploadFile formData: " + this.formData); // Output: uploadFile formData: [object FormData]
      // this.formData.keys() (or any other function) results in TypeError: dataForm.keys is not a function
  
      this.userService
          .formDataUpload(this.formData)
          .then((response: any) =&gt; {
              console.log(response);
              this.router.navigateByUrl('/');
          });
  }
  </code></pre>
  
  <p><strong>user.service.ts</strong></p>
  
  <pre><code>formDataUpload(formData: FormData): Promise&lt;any&gt; {
      return this.http.post('/api/v0/formdataupload', formData)
      .toPromise()
      .then((response: any) =&gt; {
          return response.json()
      })
      .catch(this.handleError);
  }
  </code></pre>
  
  <p><strong>server.js</strong></p>
  
  <pre><code>app.post('/api/v0/formdataupload', function(req, res) {
      console.log(req.body); // Output: {}
  });
  </code></pre>
  
  <p>How can I access the FormData object in server.js and getthe file object that was uploaded? Also, why am I unable to use any FormData functions in upload.component.ts? All the functions throw <em>TypeError: x is not a function</em>.</p>