在Angular 7中收到对HTTP post方法(上传文件)的400错误请求

时间:2020-09-16 15:26:55

标签: angular7 form-data

我有一个奇怪的东西,我找不到它的理由,我有两个Angular应用程序,在第一个应用程序中,我可以将文件正确地上传到后端,在第二个应用程序中,我使用相同的代码,并且相同的服务来上传文件,但收到错误400错误的请求,并且我不知道在哪里可以找到错误?

document.html

<div fxLayout="row" fxFlex fxFill>
<form #ajouterUnFichier=ngForm (ngSubmit)="onSubmitUnFichier(descFichier, fichier)"
    fxLayout="column" fxLayoutGap="10px" fxFlexAlign="center center" fxFlex="40%">
<mat-form-field>
  <input type="text" matInput #descFichier id="descFichier" placeholder="Entrez le nom">
</mat-form-field>

<input type="file" id="fichier" (change)="choisirFichier($event)" #fichier
       accept=".csv, .xlsx, .pdf, .doc"
/>
<div fxLayout="row" fxLayoutGap="10px">
  <button type="submit" mat-raised-button color="primary" [disabled]="!ajouterUnFichier.valid">
    Enregistrer
  </button>
  <button type="submit" mat-raised-button color="accent">
    Annuler
  </button>
</div>

在document.ts

selectedFile: File = null;

choisirFichier(event) {
if (event.target.files.length > 0) {
  this.selectedFile = <File>event.target.files[0];
 }
}
onSubmitUnFichier(descriptif: any, file: any) {
this.personneDocumentS.createFile(descriptif, this.selectedFile).subscribe(
  res => {
    if (res.status === 'error') {
      console.log('Error');
    } else {
      console.log('Sucess ************* ');
    }
  }
);
}

在document.service中

createFile(desc: string, fichier: File) {
const fd = new FormData();
fd.append('file', fichier);
fd.append('id_personne', '1');
fd.append('nom_document', 'fileName');
fd.append('descriptif', desc);
return this.httpClient.post<any>('http://localhost/silose/documents/add', fd);
}

在第一个应用程序中,一切进展顺利,但在第二个应用程序中,我有:

zone.js:3243 POST http://localhost/silose/documents/add 400 (Bad Request)

如果您有什么想法可以找到问题的地方

致谢

0 个答案:

没有答案
相关问题