Angular 2 post 请求不发送图像数据

Angular 2 post request not send image data

我正在使用表单发送图像,当它提交时发送没有数据的请求。这是我的代码:

component.html

<form [formGroup]="logoImageForm" *ngIf="representativeSelected != null" (ngSubmit)="onSubmit(logoImageForm)">
    <md-grid-list cols="6" rowHeight="50">
        <md-grid-tile>
            <input type="file" id="logo_image" formControlName="logo_image" name="logo_image" ngModel (change)="onChange($event)">
        </md-grid-tile>
    </md-grid-list>
    <md-card-actions>
        <button md-raised-button color="primary" type="submit">Save</button>
    </md-card-actions>
</form>

component.ts

onChange(event){
   this.file = event.target.files[0]; 
}

onSubmit(logoImage){
this.representativeS.uploadLogoImage(this.file)
  .subscribe(
  data => {
    swal('', 'Image updated!', 'success');
  },
  error => {
    swal('Error', '<p>' + error.message + '</p>', 'success');
  });
}

代表S(服务)

uploadLogoImage(file){
   let url = this.routesS.getApiRoute('logoImage');
   console.log(file);
   return this.http.post(url, JSON.stringify({fileData: file}),{ withCredentials: true, headers: this.headersImage })
     .map((response: Response) => {
       return response.json();
     })
     .catch(this.handleError);
}

NodeJS

var storage = multer.diskStorage({
destination: function (req, file, callback) {
    callback(null, 'path/test');
},
filename: function (req, file, callback) {
    callback(null, 'logo-image')
}
});

var upload = multer({ storage: storage }).single('logo-image');

router.route('/representatives/:id/logoImage')
   .post((req, res, next) => {
       upload(req, res, function (err) {
           if (err) {
               console.log('Error Occured');
               return;
           }
           res.end('Your File Uploaded');
       })
   });   

当我提交时,如果我检查我发送的请求,它会显示类似的内容 {fileData: {}}。为什么发送无效数据?

我认为问题在于您发送文件的方式。

另一个问题中的这个答案可能对您有帮助: