0

我有这段代码可以将文件上传到 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 标头?

4

1 回答 1

0

您应该尽量避免请求方法使用 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)
于 2020-03-28T01:34:17.183 回答