[英]ASP.NET Core 2 - Missing content-type boundary
I'm trying to upload a file from a Angular client to my ASP.NET Core 2 WebAPI service.我正在尝试将文件从 Angular 客户端上传到我的 ASP.NET Core 2 WebAPI 服务。 When I call the service, I get back an Internal Server Error.
当我调用该服务时,我得到一个内部服务器错误。 That's the error I'm getting:
这就是我得到的错误:
The component I'm using client-side is this one: ngx-uploader我在客户端使用的组件是这个: ngx-uploader
In my request options, i set them as you can see here:在我的请求选项中,我将它们设置为您可以在此处看到的:
const event: UploadInput = {
type: 'uploadAll',
url: this.printService.apiFilesBaseUrl + '/Upload',
method: 'POST',
file: this.files[0],
headers: {
'Content-Type': 'multipart/form-data',
'Accept': '*/*',
'Authorization': 'Bearer ' + this.authService.getToken()
}
};
While, server-side my controller action signature is this one:同时,服务器端我的 controller 动作签名是这个:
[HttpPost]
[Route("Upload")]
[Authorize]
public Guid Post(IFormFile file)
Breakpoints in this controller action never get hit.此 controller 操作中的断点永远不会被击中。
Can someone please share ideas about what's happening here?有人可以分享一下这里发生的事情吗?
Here, as requested, i will post my request header and payload:在这里,根据要求,我将发布我的请求 header 和有效负载:
Thanks in advance.提前致谢。
检查开发者工具网络选项卡中的上传请求,它应该具有正确的格式(匹配'Content-Type': 'multipart/form-data'
),您也可以尝试删除此标头。
You can add content-type: as multipart/form-data;您可以添加content-type: as multipart/form-data; boundary=--14737809831466499882746641449 .
边界=--14737809831466499882746641449 。
I am testing API on postman and added content-type like above it worked for me.我正在 postman 上测试 API 并添加了类似上面的内容类型,它对我有用。
For anyone having similar problem in netcore 3.1 or net 6.0, please make sure You have the Consumes attribute, ie:对于在 netcore 3.1 或 net 6.0 中遇到类似问题的任何人,请确保您具有Consumes属性,即:
[HttpPost]
[Consumes("multipart/form-data")]
public async Task<IActionResult> ImportItems(IFormFile file)
{
//...
}
On Angular 14, if you set the header like this:在 Angular 14 上,如果您像这样设置 header:
const httpOptions = { headers: new HttpHeaders().set('Content-Type', 'multipart/form-data')
};
then you should remove this header so Web Browser will add this Content-Type
header itself, ie:那么你应该删除这个 header 所以 Web 浏览器将添加这个
Content-Type
header 本身,即:
multipart/form-data;
多部分/表单数据; boundary=----WebKitFormBoundaryAGc4qL8mDa483NA2
边界=----WebKitFormBoundaryAGc4qL8mDa483NA2
Also make sure that you add enctype
attribute of html form element with value multipart/form-data
, ie:还要确保添加 html 表单元素的
enctype
属性,其值为multipart/form-data
,即:
<form #htmlForm method="post" action="#" enctype="multipart/form-data" (submit)="onFormSubmit($event)">
and decorate your asp.net core web api action with Consumes
attribute, ie:并使用
Consumes
属性装饰您的 asp.net 核心 web api 动作,即:
[HttpPost("version")]
[Consumes("multipart/form-data")]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> CreateAppVersion()
{
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.