【问题标题】:415 (Unsupported Media Type) when upload file上传文件时出现 415(不支持的媒体类型)
【发布时间】:2021-05-26 18:57:36
【问题描述】:

我编写代码以通过文件上传从表单发布数据

        [HttpPost]
        public ActionResult<Models.Event> CreateEvent(Models.Event events, IFormFile file)
        {
            string dbPath = null;
            var folderName = Path.Combine("wwwroot");
            var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);
            if (file.Length > 0)
            {
                using var image = Image.Load(file.OpenReadStream());
                image.Mutate(x => x.Resize(112, 112));
                var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                var fullPath = Path.Combine(pathToSave, fileName);
                dbPath = Path.Combine("api/event/items/pic/", fileName);
                image.Save(fullPath);
            }

            events.LogoImageFilePath = dbPath;
            _eventService.CreateEvent(events);
            return Ok(events);
        }

这是我的模型事件

public class Event 
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string LogoImageFilePath { get; set; }
}

当我使用 Postman 发布数据时

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
    "title": "Unsupported Media Type",
    "status": 415,
    "traceId": "00-e6310fde4fabb24ca3fd39a897475187-f3154d2024ff8f4f-00"
}

我该如何解决这个问题?谢谢你。 这是我的邮递员请求 标题选项卡

【问题讨论】:

  • 您发送的“Content-Type”标头的值是多少?
  • 您还应该分享您的 Postman 请求。否则,谁能帮助你?
  • 我更新了我的 Postman 请求的图片。
  • 我认为你需要用[FromBody]装饰events控制器参数
  • 这能回答你的问题吗? Upload files and JSON in ASP.NET Core Web API

标签: c# .net postman


【解决方案1】:

添加到事件 IFormFile 属性

public class Event 
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string LogoImageFilePath { get; set; }

     [NotMapped]
    [DisplayName("Upload File")]
    public IFormFile File {get; set;}
}

并更改您的操作标题:

public ActionResult<Models.Event> CreateEvent( [FromBody] Event events)
{
var file=Events.File;
....

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 2018-10-29
    • 2021-05-20
    • 2014-05-10
    • 2019-05-01
    相关资源
    最近更新 更多