简体   繁体   English

使用 Content-Type multipart/form-data 上传 IFormFile 时缺少内容类型边界

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

I have this code to upload file to aspnet core Api我有这个代码将文件上传到aspnet core Api

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

And the angular code和角度代码

   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_)...

Error: System.IO.InvalidDataException: Missing content-type boundary.错误: System.IO.InvalidDataException:缺少内容类型边界。

Commenting the /* "Content-Type": "multipart/form-data", */ line helps but since this is NswagStudio generated code, I would like to find another workaround solution.注释/* "Content-Type": "multipart/form-data", */行有帮助,但由于这是 NswagStudio 生成的代码,我想找到另一个解决方法。

Or how to tell NswagStudio not to generate this Content-Type header?或者如何告诉 NswagStudio 不要生成这个 Content-Type 标头?

You should try to avoid the request method use Post method instead.您应该尽量避免请求方法使用 Post 方法。 You can view doc here你可以在这里查看文档

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

So your code should be所以你的代码应该是

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.

相关问题 上传文件时,内容类型发送为“multipart/form-data”时找不到边界错误。如何解决此错误? - While uploading file getting error as boundary not found when content-type sent as “multipart/form-data”.How to solve this error? 如何从multipart / form-data正文请求中删除Content-Type? - How to remove Content-Type from multipart/form-data Body Request? Angular 5 &#39;Content-Type&#39;:&#39;multipart/form-data&#39; 被重置为 &#39;application/json&#39; - Angular 5 'Content-Type': 'multipart/form-data' gets reset to 'application/json' ASP.NET 核心 2 - 缺少内容类型边界 - ASP.NET Core 2 - Missing content-type boundary Angular /.NET Core API - System.InvalidOperationException:'不正确的内容类型:应用程序/表单数据' - Angular /.NET Core API - System.InvalidOperationException: 'Incorrect Content-Type: application/form-data' 使用Httpinterceptor动态设置Content-Type标头(multipart / data) - Setting Content-Type header (multipart/data) dynamically using Httpinterceptor 下载内容类型为 Content-Type:multipart/mixed 的文件 - Downloading a file with content type Content-Type:multipart/mixed 如何在angular 7中将内容类型设置为multipart/form-data - how to set content type to multipart/form-data in angular 7 在 Angular 中使用内容类型 multipart/form-data 发布 - Posting with content type multipart/form-data in Angular angular 服务调用中缺少内容类型 header - content-type header missing in angular service call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM