[英]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.