我正在尝试通过 ASP.NET Core API 上传文件。我的行动是:
[HttpPost]
[Route("{id}")]
public async Task<IActionResult> PostImage([FromBody] IFormFile file, [FromRoute] int id)
{
if(file.Length > 0)
{
var fileName = Path.GetFileName(file.FileName);
var fileExtension = Path.GetExtension(fileName);
var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images", "Image-" + id + "." + fileExtension);
using (var stream = new FileStream(path, FileMode.Create))
{
await file.CopyToAsync(stream);
}
return Ok(fileName + fileExtension);
}
return NotFound("File not found!");
}
当我使用Postman检查其功能时,遇到错误415并且Visual Studio没有跳转到断点:
{ “类型”:“https://tools.ietf.org/html/rfc7231#section-6.5.13”, "title": "不支持的媒体类型", “状态”:415, “traceId”:“00-d411f8c099a9ca4db6fe04ae25c47411-dadd8cca7cc36e45-00”}
如何在我的代码中解决这个问题?