簡體   English   中英

Angular 2發布請求不發送圖像數據

[英]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');
  });
}

代表(服務)

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

節點JS

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: {}} 為什么發送無效數據?

我認為問題在於您如何發送文件。

另一個問題中的這個答案可能會幫助您: https : //stackoverflow.com/a/35669598/5049472

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM