簡體   English   中英

使用 Content-Type multipart/form-data 上傳 IFormFile 時缺少內容類型邊界

[英]missing content-type boundary when uploading IFormFile with Content-Type multipart/form-data

我有這個代碼將文件上傳到aspnet core Api

 [HttpPost]
    [Produces(typeof(MissionBalanceWithMissionBalanceLinesModel))]
    public async Task<IActionResult> UploadBalance(IFormFile upload)
    {
        return Ok("successfully uploaded");
    }

和角度代碼

   const file: File = event.target.files[0];
   const upload= new FormData();
   fileToUpload.append('upload', file, file.name);
   const blob = fileToUpload as any
   let options_ : any = {
        body: blob,
        observe: "response",
        responseType: "blob",           
        headers: new HttpHeaders({
            /* "Content-Type": "multipart/form-data", */
            "Accept": "text/plain"
        })
    };

    return this.http.request("post", url_, options_)...

錯誤: System.IO.InvalidDataException:缺少內容類型邊界。

注釋/* "Content-Type": "multipart/form-data", */行有幫助,但由於這是 NswagStudio 生成的代碼,我想找到另一個解決方法。

或者如何告訴 NswagStudio 不要生成這個 Content-Type 標頭?

您應該盡量避免請求方法使用 Post 方法。 你可以在這里查看文檔

addHero (hero: Hero): Observable<Hero> {
  return this.http.post<Hero>(this.heroesUrl, hero, httpOptions)
    .pipe(
      catchError(this.handleError('addHero', hero))
    );
}

所以你的代碼應該是

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'multipart/form-data'
  })
};

return this.http.post(url, fileToUpload, httpOptions)

暫無
暫無

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

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