0

I write code to post data from a form with file upload

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

This is my model Event

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

When I use Postman to post data

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

How can I solve this problem? Thank you. This is my Postman request my Postman request Headers Tab Headers Tab

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
LongLong
  • 3
  • 5

1 Answers1

0

Add to Event IFormFile property

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

and change your action header:

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

}
Serge
  • 40,935
  • 4
  • 18
  • 45