【发布时间】:2020-09-29 01:38:35
【问题描述】:
我正在尝试为我的 .NET Core 3.1 Web 应用程序创建一个 POST api 控制器,以便游戏玩家可以上传他们游戏体验的屏幕截图。
这是一个稍微不同的场景,因为这个 API 将被编译成 WebGL 的 Unity 游戏使用。
所以我想我不能使用 [FromForm]。
反正我是这样写的:
[HttpPost]
public async Task<IActionResult> SubmitScreens([FromBody] IFormFile file)
{
//C:\inetpub\wwwroot\gamerProfiles\screenGrabs\
string contentRootPath = _hostingEnvironment.ContentRootPath;
// get the folder path
var uploads = Path.Combine(contentRootPath, "pic_upload_testing");
// make the file path for the uploading
var filePath = Path.Combine(uploads, file.FileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(fileStream);
}
return Ok();
}
我正在邮递员中进行测试。
我将其发送为
"Content-Type: application/octet-stream"
作为我的标题和“二进制”作为我的正文。
在 Postman 中,当我选择“二进制”时,我可以选择要使用的文件。
所以我选择了一个文件进行测试并点击发送按钮。
但我只是得到了这个:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
"title": "Unsupported Media Type",
"status": 415,
"traceId": "|dc4efc3f-45ae830112e6f778."
}
我做错了什么?
谢谢!
【问题讨论】:
-
当您通过邮递员发送数据时调试此操作并检查 Request.Body 是否包含您的数据。如果它已经删除参数并手动解析它。
标签: asp.net-core postman asp.net-core-webapi asp.net-core-3.0