繁体   English   中英

上传文件时出现 415(不支持的媒体类型)

[英]415 (Unsupported Media Type) when upload file

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

        [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);
        }

这是我的 model 活动

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"
}

我怎么解决这个问题? 谢谢你。 这是我的 Postman 请求我的邮递员请求标题选项卡标题选项卡

添加到事件 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;}
}

并更改您的操作 header:

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM