代码之家  ›  专栏  ›  技术社区  ›  Bellash

上载带有内容类型的多部分/格式文件时缺少内容类型

  •  0
  • Bellash  · 技术社区  · 6 年前

     [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不要生成这个内容类型头?

    0 回复  |  直到 6 年前
        1
  •  0
  •   Tony Ngo    6 年前

    您应该尽量避免使用请求方法而使用Post方法。您可以查看文档 here

    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)
    
    推荐文章