簡體   English   中英

將多部分/表單數據發布到端點

[英]Post multipart/form-data to endpoint

我正在嘗試為角度為6的excel文件創建一個上傳表單。我實現了一個文件選擇器,我想使用該文件選擇器將excel文件上傳(發布)到某個期望“ MULTIPART_FORM_DATA”的端點。 現在,我了解到您不應該在頭版本中為高於5的角版本設置內容類型,但是如果我在頭中不包含內容類型,則角應用程序會自動將其設置為“ application / vnd.openxmlformats-officedocument.spreadsheetml “ .sheet”,服務器不會期望它,因此會導致“錯誤請求”。 那么,如何為角度為6的multipart / form-data實現有效的“ post”?

端點看起來像這樣:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadExcel(
        @FormDataParam("file") InputStream inputStream,
        @FormDataParam("file") FormDataContentDisposition contentDispositionHeader){...}

角度分量看起來像這樣:

handleFileInput(event: any): void {
if (!event.target.files.length) {
  return;
}
this.fileToUpload = event.target.files.item(0);}

private uploadFile(): void {
    this.fileService.uploadFile(this.fileToUpload).subscribe(
      (res: any) => {
        console.log('upload succeeded');
      }
    );}

html表單看起來像這樣:

<form (submit)="uploadFile()" enctype="multipart/form-data">
<label for="file">choose excel file to upload: </label>
<input type="file" name="file" id="file" accept=".xls,.xlsx" (change)="handleFileInput($event)">
<input type="submit" name="submit" class="submitButton">

服務看起來像這樣:

uploadFile(file: File): Observable<any> {
  const fd: FormData = new FormData();
  fd.append('file', file, file.name);
  return this.http.post(this.fileURL, file);

}

我發現了自己犯的錯誤:我將錯誤的參數傳遞給http.post調用。 該服務當然應如下所示:

uploadFile(file: File): Observable<any> {
  const fd: FormData = new FormData();
  fd.append('file', file, file.name);
  return this.http.post(this.fileURL, fd);

}

暫無
暫無

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

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